@@ -4,25 +4,25 @@ def __init__(self, data):
4
4
self .next = None
5
5
6
6
7
- class Linked_List :
7
+ class LinkedList :
8
8
def __init__ (self ):
9
9
self .head = None
10
10
11
- def Insert_At_Beginning (self , new_data ):
11
+ def insert_at_beginning (self , new_data ):
12
12
new_node = Node (new_data )
13
13
if self .head is None :
14
14
self .head = new_node
15
15
return
16
16
new_node .next = self .head
17
17
self .head = new_node
18
18
19
- def Add_two_no (self , First , Second ):
19
+ def add_two_no (self , first , second ):
20
20
prev = None
21
21
temp = None
22
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
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
26
Sum = carry + first_data + second_data
27
27
carry = 1 if Sum >= 10 else 0
28
28
Sum = Sum if Sum < 10 else Sum % 10
@@ -32,37 +32,37 @@ def Add_two_no(self, First, Second):
32
32
else :
33
33
prev .next = temp
34
34
prev = temp
35
- if First is not None :
36
- First = First .next
37
- if Second is not None :
38
- Second = Second .next
35
+ if first is not None :
36
+ first = first .next
37
+ if second is not None :
38
+ second = second .next
39
39
if carry > 0 :
40
40
temp .next = Node (carry )
41
41
42
- def Display (self ):
42
+ def __str__ (self ):
43
43
temp = self .head
44
44
while temp :
45
45
print (temp .data , "->" , end = " " )
46
46
temp = temp .next
47
- print ( "None" )
47
+ return "None"
48
48
49
49
50
50
if __name__ == "__main__" :
51
- First = Linked_List ()
52
- Second = Linked_List ()
53
- First . Insert_At_Beginning (6 )
54
- First . Insert_At_Beginning (4 )
55
- First . Insert_At_Beginning (9 )
51
+ first = LinkedList ()
52
+ second = LinkedList ()
53
+ first . insert_at_beginning (6 )
54
+ first . insert_at_beginning (4 )
55
+ first . insert_at_beginning (9 )
56
56
57
- Second . Insert_At_Beginning (2 )
58
- Second . Insert_At_Beginning (2 )
57
+ second . insert_at_beginning (2 )
58
+ second . insert_at_beginning (2 )
59
59
60
60
print ("First Linked List: " )
61
- First . Display ( )
61
+ print ( first )
62
62
print ("Second Linked List: " )
63
- Second . Display ( )
63
+ print ( second )
64
64
65
- Result = Linked_List ()
66
- Result . Add_two_no ( First .head , Second .head )
65
+ result = LinkedList ()
66
+ result . add_two_no ( first .head , second .head )
67
67
print ("Final Result: " )
68
- Result . Display ( )
68
+ print ( result )
0 commit comments