Skip to content

Commit 31f5690

Browse files
Update Merge_linked_list.py
Fixes #1939 1. Introduced self.tail in __init__ to save tail node. 2. in def append, changed the code to point to new_node from tail node and then make the new_node as tail node.
1 parent 3e6b3b4 commit 31f5690

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

Merge_linked_list.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class LinkedList:
1414
# Function to initialize head
1515
def __init__(self):
1616
self.head = None
17+
self.tail = None
1718

1819
# Method to print linked list
1920
def printList(self):
@@ -29,12 +30,10 @@ def append(self, new_data):
2930

3031
if self.head is None:
3132
self.head = new_node
33+
self.tail = new_node
3234
return
33-
last = self.head
34-
35-
while last.next:
36-
last = last.next
37-
last.next = new_node
35+
self.tail.next = new_node
36+
self.tail = self.tail.next
3837

3938

4039
# Function to merge two sorted linked list.

0 commit comments

Comments
 (0)