-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2. Add Two Numbers.py
More file actions
56 lines (51 loc) · 1.44 KB
/
2. Add Two Numbers.py
File metadata and controls
56 lines (51 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode):
# initiate dummy head of return list
# p, q as head of l1, l2
# carry to be 0
dummy_head = ListNode(0)
curr = dummy_head
p = l1
q = l2
carry = 0
# loop until both p and q ends
while p or q:
x = p.val if p else 0
y = q.val if q else 0
sums = carry + x + y
carry = sum // 10
curr.next = ListNode(sum % 10)
curr = curr.next
if p:
p = p.next
if q:
q = q.next
if carry > 0:
curr.next = ListNode(carry)
return dummy_head.next
def addTwoNumbers2(self, l1: ListNode, l2: ListNode):
if not l1:
return l2
if not l2:
return l1
return self.helper(l1, l2, 0)
def helper(self, l1: ListNode, l2: ListNode, carry: int) -> ListNode:
if not l1 and not l2 and not carry:
return None
val = 0
if l1:
val += l1.val
l1 = l1.next
if l2:
val += l2.val
l2 = l2.next
if carry:
val += carry
cur = ListNode(val % 10)
carry = val // 10
cur.next = self.helper(l1, l2, carry)
return cur