Repository navigation
Expand file tree
/
Copy pathAdd Two Numers.py
More file actions
68 lines (58 loc) · 1.59 KB
/
Copy pathAdd Two Numers.py
File metadata and controls
68 lines (58 loc) · 1.59 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
64
65
66
67
68
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def insertAfter(self, prev_node, new_data):
# 1. check if the given prev_node exists
if prev_node is None:
print("The given previous node must inLinkedList.")
return
# 2. Create new node &
# 3. Put in the data
new_node = ListNode(new_data)
# 4. Make next of new Node as next of prev_node
# new_node.next = prev_node.next
# 5. make next of prev_node as new_node
prev_node.next = new_node
def addTwoNumbers(l1, l2):
addNext = 0
newVal = 0
ansL = ListNode(l1.val+l2.val%10)
l2OutOfIndex = False
l1OutOfIndex = False
while l1 != None or l2 != None:
if (l1 == None):
x = 0
l1OutOfIndex = True
elif (l2 == None):
y = 0
l2OutOfIndex = True
else:
x= l1.val
y=l2.val
temp = x + y + addNext
addNext = int(temp/10)
# print(addNext)
temp = temp%10
new = ListNode(temp)
print(temp)
print(new.val)
#
while ansL.next != None:
ansL = ansL.next
ansL.next = new
if(l1OutOfIndex == False):
l1 = l1.next
if(l2OutOfIndex == False):
l2 = l2.next
print(ansL.val)
# create ListNode
c = ListNode(3)
b = ListNode(4,c)
a = ListNode(2,b)
g = ListNode(9)
f = ListNode(4,g)
e = ListNode(6,f)
d = ListNode(5,e)
addTwoNumbers(a,d)