mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 16:27:02 +00:00
Merge pull request #182 from tonydelanuez/patch-1
Adding reverse() to singly-linked list
This commit is contained in:
commit
6bdf4fc2d1
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user