fix:键盘问题

上级 09a03eb2
无相关合并请求
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (py36tf1)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (python-algorithm)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
......@@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.9 (py36tf1)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.9 (python-algorithm)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
"""
两数相加 II
"""
from typing import List, Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# 翻转链表
def reverseList(head: ListNode) -> ListNode:
pre = None
curr = head
while curr:
next = curr.next
curr.next = pre
pre = curr
curr = next
return pre
class Solution:
def addTwoNumbers(self, l11: Optional[ListNode], l22: Optional[ListNode]) -> Optional[ListNode]:
l1 = reverseList(l11)
l2 = reverseList(l22)
res = ListNode(0)
cur = res
carry = 0
while l1 or l2:
x = l1.val if l1 else 0
y = l2.val if l2 else 0
s = x + y + carry
carry = s // 10
cur.next = ListNode(s % 10)
cur = cur.next
if l1:
l1 = l1.next
if l2:
l2 = l2.next
if carry:
cur.next = ListNode(carry)
return reverseList(res.next)
if __name__ == '__main__':
l1 = ListNode(7)
l1.next = ListNode(2)
l1.next.next = ListNode(4)
l1.next.next.next = ListNode(3)
# l1.next.next.next.next = ListNode(9)
# l1.next.next.next.next.next = ListNode(9)
# l1.next.next.next.next.next.next = ListNode(9)
l2 = ListNode(5)
l2.next = ListNode(6)
l2.next.next = ListNode(4)
# l2.next.next.next = ListNode(9)
result = Solution().addTwoNumbers(l1, l2)
while result:
print(result.val, end=' -> ')
result = result.next
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册
反馈
建议
客服 返回
顶部