forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add_two_Linked_List.py
68 lines (58 loc) · 1.84 KB
/
Add_two_Linked_List.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
68
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Linked_List:
def __init__(self):
self.head = None
def Insert_At_Beginning(self, new_data):
new_node = Node(new_data)
if self.head is None:
self.head = new_node
return
new_node.next = self.head
self.head = new_node
def Add_two_no(self, First, Second):
prev = None
temp = None
carry = 0
while First is not None or Second is not None:
first_data = 0 if First is None else First.data
second_data = 0 if Second is None else Second.data
Sum = carry+first_data+second_data
carry = 1 if Sum >= 10 else 0
Sum = Sum if Sum < 10 else Sum % 10
temp = Node(Sum)
if self.head is None:
self.head = temp
else:
prev.next = temp
prev = temp
if First is not None:
First = First.next
if Second is not None:
Second = Second.next
if carry > 0:
temp.next = Node(carry)
def Display(self):
temp = self.head
while(temp):
print(temp.data, "->", end=" ")
temp = temp.next
print("None")
if __name__ == "__main__":
First = Linked_List()
Second = Linked_List()
First.Insert_At_Beginning(6)
First.Insert_At_Beginning(4)
First.Insert_At_Beginning(9)
Second.Insert_At_Beginning(2)
Second.Insert_At_Beginning(2)
print("First Linked List: ")
First.Display()
print("Second Linked List: ")
Second.Display()
Result = Linked_List()
Result.Add_two_no(First.head, Second.head)
print("Final Result: ")
Result.Display()