## DEV Community

Tamerlan Gudabayev

Posted on • Updated on

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.

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:

1. Head: The first element of a linked list (in this case it is the element at memory address 3200)
2. Content: Your data (can be anything you'd want ex. string, ints, floats, etc...)
3. Pointer: Address in memory to the next element (node).
4. 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

There are three types of linked lists:

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).

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.

## 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.

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:

1. `list` - Display all elements/nodes.
2. `push` - insert at the end of the list.
3. `insertAtBeginning` - insert at the beginning of the list
4. `insert` - inserting in between two Data Nodes
5. `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):
``````

Let's implement the methods one by one.

### Display all elements

``````class SLinkedList:
def __init__(self):

def list(self):
while printval is not None:
print (printval.dataval)
printval = printval.nextval
``````

### Insert at the beginning of the list

``````class SLinkedList:
def __init__(self):

def insertAtBeginning(self,newdata):
NewNode = Node(newdata)

# Update the new nodes next val to existing node
``````

### Insert at the end of the list

``````class SLinkedList:
def __init__(self):

def push(self, newdata):
NewNode = Node(newdata)
return
while(laste.nextval):
laste = laste.nextval
laste.nextval=NewNode
``````

### Insert between two nodes

``````class SLinkedList:
def __init__(self):

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):

def remove(self, Removekey):

return

break

return

``````

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?

Now, coming back to our main question.

How do we reverse a linked list?

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:

1. Initialize three pointers `prev` as NULL, `curr` as head and `next` as NULL.
2. 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

def __init__(self):

# Function to reverse the linked list
def reverse(self):
prev = None
while(current is not None):
next = current.next
current.next = prev
prev = current
current = next

# Function to insert a new node at the beginning
def push(self, new_data):
new_node = Node(new_data)

def printList(self):
while(temp):
print temp.data,
temp = temp.next

# Driver code
llist.push(20)
llist.push(4)
llist.push(15)
llist.push(85)

llist.printList()
llist.reverse()
llist.printList()

# This code is contributed by Nikhil Kumar Singh(nickzuck_007)
``````

## Conclusion

In summary today we learned:

1. Linked Lists and their properties.
2. Where it is used
3. Implementation
4. 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.