How to reverse a linked list?
You probably heard this question a billion times already, may it be from coding interviews, colleagues, and even some YouTube influencers saying you aint a programmer if you don't know how to reverse a linked list (talking to you TechLead).
No worries, there's no shame in not knowing that.
However, it is my duty as your data structures teacher, to teach you that and pass on knowledge from one programmer to another.
So without further ado, let's master the linked list.
Table of Contents
- Demystifying Linked Lists
- Where the hell are they used?
- Types of Linked Lists
- Complexity
- Implementations of Linked Lists
- How to reverse a god damn linked list?
- Conclusion
- Additional Resources
Demystifying Linked Lists
When I first started studying data structures, I was pretty terrified of linked lists.
I don't know what it is, but it sounds so complicated.
But boy was I wrong.
It's actually far simpler than I thought.
So to begin with, let us define the linked list.
- A linked list is a data structure.
- It's linear, meaning elements are in a sequence (ex. 1 2 3 4 5).
- Elements of a linked list are NOT stored sequentially to each other in memory(RAM).
- Elements are linked to each other's using pointers.
Below is an image visualizing linked lists:
Let us break this image down:
- Head: The first element of a linked list (in this case it is the element at memory address 3200)
- Content: Your data (can be anything you'd want ex. string, ints, floats, etc...)
- Pointer: Address in memory to the next element (node).
- Tail: Last element of a linked list (you can identify a tail by it having always a pointer of next element to null)
Where the hell are they used?
As programmers, our first instinct is to see how we can use this in our current or new projects, and this is valid thinking.
We don't wanna be studying something that we might never use.
Right?
Well, that is why I've compiled down a list of use-cases for linked lists.
- Implementation of Stacks and Queues (FIFO)
- Implementation of Graphs
- Dynamic memory allocation
- Maintaining directory of names
- Performing arithmetic operations on long integers
- Manipulation of Polynomials
- Representing sparse matrices
Types of Linked Lists
There are three types of linked lists:
Single Linked List
A singly linked list is a type of linked list that is unidirectional, that is, it can be traversed in only one direction from head to the last node(tail).
Doubly Linked List
A doubly linked list is a type of linked list that is bidirectional, that is, it can be traversed in two directions, from head to the last node(tail) and vice-versa.
Circular Linked List
A circular linked list is a type of linked list, in which the head points to the tail, and the tail points to the head. Both Singly Linked List and Doubly Linked List can be made into a circular linked list.
Complexity
Keep in mind when measuring the complexity of a data structure, we always look at the worst-case scenario. So we can imagine that the size of this linked list is in the millions or even billions.
Access
Both single and doubly linked list has to traverse the whole linked list to find the specific element that we are searching for. That is why the complexity for is O(n).
Search
Search is similar to access time, so it is also O(n).
Insertion
Linked lists are used best in insertion and deletion due to the elements not needing to be explicitly sequential in memory.
Insertion is pretty simple.
For example, if you want to add an element to the beginning of a linked list(head). You simply create the element in some memory location and you point it to the previous head. This is why the time complexity is O(1).
Deletion
Similar to insertion, the time complexity is O(1), because to delete your not gonna have to go through the whole list, shifting elements. You simply manipulate the pointers of the previous and next element that you want to delete.
Implementation of Linked List
Usually, to represent a node in a linked list, we create a class with these properties:
class Node:
def __init__(self, data=None):
self.data = data
self.nextval = None
- Data: value of the data (string, int, etc...).
- Nextval: pointer or reference to the next element in the list.
The standard linked list interface requires us to implement these methods:
-
list
- Display all elements/nodes. -
push
- insert at the end of the list. -
insertAtBeginning
- insert at the beginning of the list -
insert
- inserting in between two Data Nodes -
remove
- remove an existing node using the key for that node
Our base class will look like this:
class SLinkedList: # Single Linked List
def __init__(self):
self.headval = None
Let's implement the methods one by one.
Display all elements
class SLinkedList:
def __init__(self):
self.headval = None
def list(self):
printval = self.headval
while printval is not None:
print (printval.dataval)
printval = printval.nextval
Insert at the beginning of the list
class SLinkedList:
def __init__(self):
self.headval = None
def insertAtBeginning(self,newdata):
NewNode = Node(newdata)
# Update the new nodes next val to existing node
NewNode.nextval = self.headval
self.headval = NewNode
Insert at the end of the list
class SLinkedList:
def __init__(self):
self.headval = None
# Function to add newnode
def push(self, newdata):
NewNode = Node(newdata)
if self.headval is None:
self.headval = NewNode
return
laste = self.headval
while(laste.nextval):
laste = laste.nextval
laste.nextval=NewNode
Insert between two nodes
class SLinkedList:
def __init__(self):
self.headval = None
def insertBetween(self,middle_node,newdata):
if middle_node is None:
print("The mentioned node is absent")
return
NewNode = Node(newdata)
NewNode.nextval = middle_node.nextval
middle_node.nextval = NewNode
Remove a node using it's key
class SLinkedList:
def __init__(self):
self.head = None
def remove(self, Removekey):
HeadVal = self.head
if (HeadVal is not None):
if (HeadVal.data == Removekey):
self.head = HeadVal.next
HeadVal = None
return
while (HeadVal is not None):
if HeadVal.data == Removekey:
break
prev = HeadVal
HeadVal = HeadVal.next
if (HeadVal == None):
return
prev.next = HeadVal.next
HeadVal = None
Keep in mind, here we are implementing a single-linked list. Implementing a double-linked list is similar, apart from the extra prev
variable which will store the pointer or reference to the previous element. It will look something like this.
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
self.prev = None
How to reverse a god damn linked list?
Congratulations, you know about linked lists.
Now, coming back to our main question.
How do we reverse a linked list?
Think about it, and then proceed to read the answer below.
To help you imagine this:
Input: 1→ 2→ 3→ 4
Output: 4 → 3 → 2 → 1
Solution
There are multiple ways to reverse a linked list, which includes:
- Iterative Method
- Recursive Method
- Stack Method
For the sake of simplicity, we are going to use the iterative method.
Pseudocode:
- Initialize three pointers
prev
as NULL,curr
as head andnext
as NULL. - Iterate through the linked list. In the loop, do the following:
// Before changing next of current,
// store next node
next = curr->next
// Now change next of current
// This is where actual reversing happens
curr->next = prev
// Move prev and curr one step forward
prev = curr
curr = next
Python Implementation
# Python program to reverse a linked list
# Time Complexity: O(n)
# Space Complexity: O(1)
# Node class
class Node:
# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
# Function to initialize head
def __init__(self):
self.head = None
# Function to reverse the linked list
def reverse(self):
prev = None
current = self.head
while(current is not None):
next = current.next
current.next = prev
prev = current
current = next
self.head = prev
# Function to insert a new node at the beginning
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
# Utility function to print the linked LinkedList
def printList(self):
temp = self.head
while(temp):
print temp.data,
temp = temp.next
# Driver code
llist = LinkedList()
llist.push(20)
llist.push(4)
llist.push(15)
llist.push(85)
print "Given Linked List"
llist.printList()
llist.reverse()
print "\nReversed Linked List"
llist.printList()
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)
Conclusion
In summary today we learned:
- Linked Lists and their properties.
- Where it is used
- Implementation
- How to reverse one
I hope you learned something today, and if you got any questions/suggestions feel free to comment them down in the description.
Top comments (2)
Good explanation. Thanks!
Thank you!!