forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-two-numbers-ii.py
More file actions
executable file
·62 lines (57 loc) · 2.07 KB
/
add-two-numbers-ii.py
File metadata and controls
executable file
·62 lines (57 loc) · 2.07 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
"""
First, if you haven't done the [first problem](https://leetcode.com/problems/add-two-numbers/) please do it first. And here is its [answer](https://leetcode.com/problems/add-two-numbers/discuss/43105).
We can just reverse the linked list, and we can use the solution of the [first problem](https://leetcode.com/problems/add-two-numbers/).
So I do this problem without reversing the linked list.
We need to know the length of each linked list. So we can know when do we start adding the `val`.
We need to set the answer to a double linked list. Because we might need to modify the upper digits.
For example 999 + 1.
The time complexity is `O(N)`, `N` is the length of the input linked list.
The space complexity is `O(N)`, too.
"""
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
self.prev = None
class Solution(object):
def addTwoNumbers(self, A, B):
l1 = self.getLength(A)
l2 = self.getLength(B)
head = ListNode(0)
curr = head
while A or B:
if l1>l2:
self.addDigit(curr, A.val)
l1-=1
A = A.next
elif l2>l1:
self.addDigit(curr, B.val)
l2-=1
B = B.next
else:
val = (A.val if A else 0) + (B.val if B else 0)
self.addDigit(curr, val)
if l1: l1-=1
if l2: l2-=1
if A: A = A.next
if B: B = B.next
curr = curr.next
return head if head.val else head.next #remove leading zero
def getLength(self, L):
length = 0
while L:
length+=1
L = L.next
return length
def addDigit(self, node, val):
if val<10:
node.next = ListNode(val)
node.next.prev = node
else:
node.next = ListNode(val-10)
node.next.prev = node
node.val+=1
while node.val>=10:
node.prev.val+=1
node.val-=10
node = node.prev