forked from priyankchheda/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum_two_lists.py
88 lines (69 loc) · 2.41 KB
/
sum_two_lists.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
""" You are given two non-empty linked lists representing two non-negative
integers. The digits are stored in reverse order, and each of their
nodes contains a single digit. Add the two numbers and return the sum
as a linked list.
You may assume the two numbers do not contain any leading zero,
except the number 0 itself.
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
"""
class Node:
""" Node class contains everything related to Linked List node """
def __init__(self, data):
""" initializing single node with data """
self.data = data
self.next = None
class LinkedList:
""" Singly Linked List is a linear data structure """
def __init__(self):
""" initializing singly linked list with zero node """
self.head = None
def insert_head(self, data):
""" inserts node at the start of linked list """
node = Node(data)
node.next = self.head
self.head = node
def print(self):
""" prints entire linked list without changing underlying data """
current = self.head
while current is not None:
print(" ->", current.data, end="")
current = current.next
print()
def add_two_numbers(list1, list2):
""" add two numbers which are represented by linkedlist """
carry = 0
current1 = list1.head
current2 = list2.head
result = LinkedList()
result.insert_head(0)
result_pointer = result.head
while current1 or current2:
val1 = getattr(current1, 'data', 0)
val2 = getattr(current2, 'data', 0)
temp_val = carry + val1 + val2
carry = temp_val // 10
result_pointer.next = Node(temp_val % 10)
result_pointer = result_pointer.next
current1 = getattr(current1, 'next', None)
current2 = getattr(current2, 'next', None)
if carry > 0:
result_pointer.next = Node(carry)
result.head = result.head.next
return result
def main():
""" operational function """
linkedlist1 = LinkedList()
linkedlist1.insert_head(3)
linkedlist1.insert_head(6)
linkedlist1.insert_head(5)
linkedlist1.print()
linkedlist2 = LinkedList()
linkedlist2.insert_head(2)
linkedlist2.insert_head(4)
linkedlist2.insert_head(8)
linkedlist2.print()
add_two_numbers(linkedlist1, linkedlist2).print()
if __name__ == '__main__':
main()