-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path002-add-two-numbers..py
More file actions
69 lines (67 loc) · 2.12 KB
/
002-add-two-numbers..py
File metadata and controls
69 lines (67 loc) · 2.12 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
57
58
59
60
61
62
63
64
65
66
67
68
69
class Solution_me:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
addnext = 0
sumval = 0
res = ListNode()
cur = res
while l1 != None and l2 != None:
sumval = addnext + l1.val + l2.val
cur.next = ListNode(sumval % 10)
addnext = sumval // 10
cur = cur.next
l1 = l1.next
l2 = l2.next
while l1 != None:
sumval = addnext + l1.val
cur.next = ListNode(sumval % 10)
addnext = sumval // 10
cur = cur.next
l1 = l1.next
while l2 != None:
sumval = addnext + l2.val
cur.next = ListNode(sumval % 10)
addnext = sumval // 10
cur = cur.next
l2 = l2.next
if addnext != 0:
cur.next = ListNode(addnext)
return res.next
class Solution_Github:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
addnext = 0
sumval = 0
res = ListNode()
cur = res
while l1 != None or l2 != None:
if l1 != None:
sumval += l1.val
l1 = l1.next
if l2 != None:
sumval += l2.val
l2 = l2.next
cur.next = ListNode(sumval % 10)
addnext = sumval // 10
cur = cur.next
if addnext != 0:
cur.next = ListNode(addnext)
return res.next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode()
cur = dummy
borrow = 0
while l1 or l2 or borrow:
sum_val = 0
if l1:
sum_val += l1.val
l1 = l1.next
if l2:
sum_val += l2.val
l2 = l2.next
if borrow:
sum_val += borrow
value = sum_val % 10
borrow = sum_val // 10
cur.next = ListNode(val=value)
cur = cur.next
return dummy.next