Skip to content

Commit 4aabf9b

Browse files
Gaurav JaiswalGaurav Jaiswal
authored andcommitted
Fix an error in insert method of LinkedList and Add new reverse method in linkedList
1 parent c88895a commit 4aabf9b

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

Data Structures and Algorithms/linked_list.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def insert(self, data, index):
7777
current = self.head
7878

7979
while position > 1:
80-
current = node.next_node
80+
current = current.next_node
8181
position -= 1
8282

8383
prev_node = current
@@ -139,3 +139,21 @@ def __repr__(self):
139139

140140
current = current.next_node
141141
return "->".join(nodes)
142+
143+
def reverse(self):
144+
"""
145+
reverses the complete linked list
146+
returns the linked list
147+
the time complexity of it is 0(n) time.
148+
"""
149+
first = self.head
150+
second = self.head.next_node
151+
152+
while(second != None):
153+
store_rest_list = second.next_node
154+
second.next_node = first
155+
first = second
156+
second = store_rest_list
157+
self.head.next_node = None
158+
self.head = first
159+
return self

0 commit comments

Comments
 (0)