Skip to content

Commit dda3937

Browse files
finished 2.5
1 parent fcbd0f2 commit dda3937

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
"""
2+
Sum Lists: You have two numbers represented by a linked list, where each node
3+
contains a single digit.
4+
The digits are stored in reverse order, such that the 1 's digit is at the head
5+
of the list. Write a function that adds the two numbers and returns the sum as
6+
a linked list.
7+
8+
EXAMPLE
9+
Input:(7-> 1 -> 6) + (5 -> 9 -> 2).That is,617 + 295. Output:2 -> 1 -> 9.That is,912.
10+
FOLLOW UP
11+
Suppose the digits are stored in forward order. Repeat the above problem. EXAMPLE
12+
lnput:(6 -> 1 -> 7) + (2 -> 9 -> 5).That is, 617 + 295. Output:9 -> 1 -> 2.Thatis,912.
13+
Hints: #7, #30, #71, #95, #109
14+
15+
16+
USE THIS IF YOUR SUPERDUPER STUCK
17+
https://www.youtube.com/watch?v=dm9drjU-DuM
18+
19+
"""
20+
21+
22+
23+
class Node:
24+
#Singly link list
25+
def __init__(self,data = None):
26+
self.data = data
27+
self.next = None
28+
29+
class linklist:
30+
#linkList class
31+
def __init__(self):
32+
self.head = None
33+
self.size = 0
34+
35+
def push(self,data):
36+
node = Node(data)# create a new node
37+
38+
if self.head == None: #check to see if the head node is empty
39+
self.head = node # If its empty add it to the new node
40+
self.size = 1
41+
return
42+
#if the head of the linklist is filled
43+
44+
current = self.head
45+
while current.next is not None:#Check the current postion is empty
46+
#Move to the next line if nothing is there
47+
current = current.next
48+
49+
50+
current.next = node #point self.head to a new node
51+
self.size+=1
52+
53+
def lenght(self):
54+
#note the count doesn't start at zero
55+
cur = self.head
56+
counter = 0
57+
while cur is not None:
58+
counter+=1
59+
cur = cur.next
60+
print('Linklist len: '+str(counter))
61+
return counter
62+
63+
def printList(self):
64+
curr = self.head
65+
elem = []
66+
67+
while(curr != None):
68+
elem.append(curr.data)
69+
curr = curr.next
70+
print(elem)
71+
#1->2->3
72+
def remove_node(self,data):
73+
#1->2->3
74+
curr = self.head
75+
if curr is not None and curr.data == data:
76+
self.head = curr.next
77+
curr = None
78+
#look for the node that has the data we are looking for
79+
while curr is not None:
80+
if curr.data == data:
81+
break
82+
prev = curr
83+
curr = curr.next
84+
85+
#if data isn't found just reutrn
86+
if(curr == None):
87+
return
88+
89+
#allow us to unlink the nodes
90+
prev.next = curr.next
91+
curr = None
92+
93+
94+
def sum_forward_backwards(self,llist_B):
95+
if llist_B.head == None:
96+
return "llist_B is None"
97+
98+
sum_list = linklist()
99+
head_b = llist_B.head
100+
curr = self.head #List A
101+
carry = 0
102+
while curr or head_b:
103+
if curr == None and head_b == None:
104+
i,j = 0
105+
else:
106+
i = curr.data
107+
j = head_b.data
108+
109+
sum_ = i + j + carry
110+
if sum_ >= 10:
111+
112+
carry = 1
113+
r = sum_ % 10
114+
sum_list.push(r)
115+
116+
else:
117+
carry = 0
118+
sum_list.push(sum_)
119+
if curr:
120+
curr = curr.next
121+
if head_b:
122+
head_b = head_b.next
123+
124+
self.head = sum_list.head
125+
126+
127+
128+
129+
130+
link_A = linklist()
131+
link_B = linklist()
132+
link_A.push(7)
133+
link_A.push(1)
134+
link_A.push(6)
135+
136+
link_B.push(5)
137+
link_B.push(9)
138+
link_B.push(2)
139+
link_A.sum_forward_backwards(link_B)
140+
link_A.printList()

0 commit comments

Comments
 (0)