forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-two-numbers-ii.py
More file actions
63 lines (60 loc) · 1.69 KB
/
add-two-numbers-ii.py
File metadata and controls
63 lines (60 loc) · 1.69 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
"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param l1: The first list.
@param l2: The second list.
@return: the sum list of l1 and l2.
"""
def addLists2(self, l1, l2):
# write your code here
head, tail = None, None
step = 0
l1 = self.reverse(l1)
l2 = self.reverse(l2)
while l1 and l2:
val = l1.val + l2.val + step
step = int(val / 10)
val = val % 10
if not head:
head = tail = ListNode(val)
else:
tail.next = ListNode(val)
tail = tail.next
l1 = l1.next
l2 = l2.next
if l1 or l2:
l = l1 if l1 else l2
while l:
val = l.val + step
step = int(val / 10)
val = val % 10
if not head:
head = tail = ListNode(val)
else:
tail.next = ListNode(val)
tail = tail.next
l = l.next
if step:
tail.next = ListNode(step)
tail = tail.next
return self.reverse(head)
def reverse(self, node):
head, tail = None, None
while node:
if not head:
head, tail = node, node
node = node.next
tail.next = None
else:
tmp = node.next
node.next = head
head = node
node = tmp
return head
# medium: https://www.lintcode.com/problem/add-two-numbers-ii/