Skip to content

Commit 7ab913a

Browse files
Merge pull request #1905 from IMperiumX/refactor
refactor: improve readability for LinkedList class
2 parents 48a8974 + 72f022d commit 7ab913a

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

Diff for: Add_two_Linked_List.py

+24-24
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ def __init__(self, data):
44
self.next = None
55

66

7-
class Linked_List:
7+
class LinkedList:
88
def __init__(self):
99
self.head = None
1010

11-
def Insert_At_Beginning(self, new_data):
11+
def insert_at_beginning(self, new_data):
1212
new_node = Node(new_data)
1313
if self.head is None:
1414
self.head = new_node
1515
return
1616
new_node.next = self.head
1717
self.head = new_node
1818

19-
def Add_two_no(self, First, Second):
19+
def add_two_no(self, first, second):
2020
prev = None
2121
temp = None
2222
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
2626
Sum = carry + first_data + second_data
2727
carry = 1 if Sum >= 10 else 0
2828
Sum = Sum if Sum < 10 else Sum % 10
@@ -32,37 +32,37 @@ def Add_two_no(self, First, Second):
3232
else:
3333
prev.next = temp
3434
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
3939
if carry > 0:
4040
temp.next = Node(carry)
4141

42-
def Display(self):
42+
def __str__(self):
4343
temp = self.head
4444
while temp:
4545
print(temp.data, "->", end=" ")
4646
temp = temp.next
47-
print("None")
47+
return "None"
4848

4949

5050
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)
5656

57-
Second.Insert_At_Beginning(2)
58-
Second.Insert_At_Beginning(2)
57+
second.insert_at_beginning(2)
58+
second.insert_at_beginning(2)
5959

6060
print("First Linked List: ")
61-
First.Display()
61+
print(first)
6262
print("Second Linked List: ")
63-
Second.Display()
63+
print(second)
6464

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)
6767
print("Final Result: ")
68-
Result.Display()
68+
print(result)

0 commit comments

Comments
 (0)