Merge pull request #182 from tonydelanuez/patch-1

Adding reverse() to singly-linked list
This commit is contained in:
Harshil 2017-10-25 14:12:58 +05:30 committed by GitHub
commit 6bdf4fc2d1

View File

@ -47,4 +47,20 @@ class Linked_List:
return Head
def isEmpty(Head):
return Head is None #Return if Head is none
return Head is None #Return if Head is none
def reverse(Head):
prev = None
current = Head
while(current):
# Store the current node's next node.
next_node = current.next
# Make the current node's next point backwards
current.next = prev
# Make the previous node be the current node
prev = current
# Make the current node the next node (to progress iteration)
current = next_node
# Return prev in order to put the head at the end
Head = prev