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

Method add_two_no

Add_two_Linked_List.py:19–40  ·  view source on GitHub ↗
(self, first, second)

Source from the content-addressed store, hash-verified

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

Callers 1

Calls 1

NodeClass · 0.70

Tested by

no test coverage detected