We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a2ee4f3 commit aed9746Copy full SHA for aed9746
Kangli/LinkedList/addTwoNumbers.py
@@ -0,0 +1,26 @@
1
+class ListNode(object):
2
+ def __init__(self, x):
3
+ self.val = x
4
+ self.next = None
5
+
6
+class Solution(object):
7
+ def addTwoNumbers(self, l1, l2):
8
+ dummy = cur = ListNode(0)
9
+ carry = 0
10
+ while l1 or l2 or carry:
11
+ if l1:
12
+ carry += l1.val
13
+ l1 = l1.next
14
+ if l2:
15
+ carry += l2.val
16
+ l2 = l2.next
17
+ cur.next = ListNode(carry%10)
18
+ cur = cur.next
19
+ carry //= 10
20
+ return dummy.next
21
+s = Solution()
22
+l1 = ListNode(2)
23
+l1.next = ListNode(4)
24
+l2 = ListNode(5)
25
+l2.next = ListNode(6)
26
+s.addTwoNumbers(l1, l2)
0 commit comments