forked from chiphuyen/coding-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc_add_number_reverse.py
60 lines (47 loc) · 1.46 KB
/
lc_add_number_reverse.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
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
'''
This is a problem on leetcode.com
https://leetcode.com/problems/add-two-numbers/description/
You are given two non-empty linked lists of non-negative integers.
The digits are stored in reverse order and each of their nodes
contain a single digit.
Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero,
except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
'''
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def _addHelper(self, l1, l2, head, curr, carry_over):
if not l1 and not l2:
if carry_over == 1:
curr.next = ListNode(1)
return head
if not l1:
val = l2.val + carry_over
l2 = l2.next
elif not l2:
val = l1.val + carry_over
l1 = l1.next
else:
val = l1.val + l2.val + carry_over
l2 = l2.next
l1 = l1.next
next_node = ListNode(val % 10)
if not head:
head = next_node
curr = head
else:
curr.next = next_node
curr = next_node
return self._addHelper(l1, l2, head, curr, val // 10)
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
return self._addHelper(l1, l2, None, None, 0)