-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddTwoNumbers
More file actions
56 lines (43 loc) · 1.48 KB
/
addTwoNumbers
File metadata and controls
56 lines (43 loc) · 1.48 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
class ListNode:
def __init__(self, data):
self.data = data
self.next = None
class Solution:
def __init__(self):
self.head = None
def push(self, new_data):
new_node = ListNode(new_data)
new_node.next = self.head
self.head = new_node
def addTwoNumbers(self, l1, l2):
dummyHead = ListNode(0)
tail = dummyHead
carry = 0
while l1 is not None or l2 is not None or carry != 0:
digit1 = 0 if l1 is None else l1
digit2 = 0 if l2 is None else l2
sum = digit1 + digit2 + carry #ERROR HERE with my code and using a driver from google search
digit = sum % 10
carry = sum // 10
new_node = ListNode(digit)#create new node
tail.next = new_node#add new node to tail node
tail = tail.next#move the pointer to new node
def printList(self):
temp = self.head
while(temp):
print (temp.data,end=' ')
temp = temp.next
# result = dummyHead.next
# dummyHead.next = None
# return result
l1 = Solution() #got this wrong in my approach
l2 = Solution() #got this wrong in my approach
l1.push(2)
l1.push(4)
l1.push(7)
l1.push(5)
l1.push(6)
l1.push(4)
result = Solution()
result.addTwoNumbers(l1,l2)
result.printList