From 226a0a4ad8d382a5f6db102ded889b7455731df4 Mon Sep 17 00:00:00 2001 From: Tony De La Nuez Date: Sun, 22 Oct 2017 18:04:10 -0500 Subject: [PATCH] Adding reverse() to singly-linked list Updating singly-linked list to include a reverse method using the three-way shuffle method. --- .../LinkedList/singly_LinkedList.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/data_structures/LinkedList/singly_LinkedList.py b/data_structures/LinkedList/singly_LinkedList.py index c9a3cec27..8828ce6cc 100644 --- a/data_structures/LinkedList/singly_LinkedList.py +++ b/data_structures/LinkedList/singly_LinkedList.py @@ -47,4 +47,20 @@ class Linked_List: return Head def isEmpty(Head): - return Head is None #Return if Head is none \ No newline at end of file + 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