-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC_Add_Two_Numbers.py
67 lines (60 loc) · 1.74 KB
/
LC_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
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
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
"""
https://leetcode.com/problems/add-two-numbers-ii/description/
Loop through the elements of the linked list. push it to a stack.
pop it out and read it and store it in a list
"""
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
List_l1 = []
List_l2 = []
if ((l1 is None) and (l2 is None)):
return List_l1
while (l1):
List_l1.append(l1.val)
l1 = l1.next
while (l2):
List_l2.append(l2.val)
l2 = l2.next
if (len(List_l1) == 0):
return List_l2
if (len(List_l2) == 0):
return List_l1
carry = 0;
Answer = []
while (len(List_l1)>0 and len(List_l2)>0):
sum = List_l1.pop() + List_l2.pop() + carry
if (sum >= 10):
carry = 1
else:
carry = 0
sum = sum % 10
Answer.insert(0,sum)
while (len(List_l1)>0):
sum = List_l1.pop() + carry
if (sum >= 10):
carry = 1
else:
carry = 0
sum = sum % 10
Answer.insert(0,sum)
while (len(List_l2)>0):
sum = List_l2.pop() + carry
if (sum >= 10):
carry = 1
else:
carry = 0
sum = sum % 10
Answer.insert(0,sum)
if (carry == 1):
Answer.insert(0,1)
return (Answer)