Skip to content

Commit 226a0a4

Browse files
authored
Adding reverse() to singly-linked list
Updating singly-linked list to include a reverse method using the three-way shuffle method.
1 parent 535cbb7 commit 226a0a4

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

data_structures/LinkedList/singly_LinkedList.py

Lines changed: 17 additions & 1 deletion
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)