Skip to content

Commit 6bdf4fc

Browse files
authored
Merge pull request TheAlgorithms#182 from tonydelanuez/patch-1
Adding reverse() to singly-linked list
2 parents 1aa3717 + 226a0a4 commit 6bdf4fc

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

data_structures/LinkedList/singly_LinkedList.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,20 @@ def delete_tail(Head):#delete from tail
4747
return Head
4848

4949
def isEmpty(Head):
50-
return Head is None #Return if Head is none
50+
return Head is None #Return if Head is none
51+
52+
def reverse(Head):
53+
prev = None
54+
current = Head
55+
56+
while(current):
57+
# Store the current node's next node.
58+
next_node = current.next
59+
# Make the current node's next point backwards
60+
current.next = prev
61+
# Make the previous node be the current node
62+
prev = current
63+
# Make the current node the next node (to progress iteration)
64+
current = next_node
65+
# Return prev in order to put the head at the end
66+
Head = prev

0 commit comments

Comments
 (0)