forked from YaxeZhang/Just-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-two-numbers.py
34 lines (28 loc) · 898 Bytes
/
add-two-numbers.py
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
flag = 0
dummy = p1 = ListNode(0); p2 = ListNode(0)
p1.next , p2.next = l1, l2
while p1.next and p2.next:
p1, p2 = p1.next, p2.next
sumval = p1.val + p2.val + flag
p1.val = sumval % 10
flag = sumval >= 10
p1.next = p1.next or p2.next
while flag and p1.next:
p1 = p1.next
if p1.val == 9:
p1.val = 0
else:
p1.val += 1
flag = 0
if flag:
while p1.next:
p1 = p1.next
p1.next = ListNode(1)
return dummy.next