MCPcopy Create free account
hub / github.com/geekcomputers/Python / LinkedList

Class LinkedList

Add_two_Linked_List.py:7–47  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5
6
7class LinkedList:
8 def __init__(self):
9 self.head = None
10
11 def insert_at_beginning(self, new_data):
12 new_node = Node(new_data)
13 if self.head is None:
14 self.head = new_node
15 return
16 new_node.next = self.head
17 self.head = new_node
18
19 def add_two_no(self, first, second):
20 prev = None
21 temp = None
22 carry = 0
23 while first is not None or second is not None:
24 first_data = 0 if first is None else first.data
25 second_data = 0 if second is None else second.data
26 Sum = carry + first_data + second_data
27 carry = 1 if Sum >= 10 else 0
28 Sum = Sum if Sum < 10 else Sum % 10
29 temp = Node(Sum)
30 if self.head is None:
31 self.head = temp
32 else:
33 prev.next = temp
34 prev = temp
35 if first is not None:
36 first = first.next
37 if second is not None:
38 second = second.next
39 if carry > 0:
40 temp.next = Node(carry)
41
42 def __str__(self):
43 temp = self.head
44 while temp:
45 print(temp.data, "->", end=" ")
46 temp = temp.next
47 return "None"
48
49
50if __name__ == "__main__":

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected