Linked list reversal
A linked list is a linear data structure. A node in a linked list consists of:
- Data item.
- The address of the next node.
class Node
{
int data;
Node *next;
};
This article will show you how to reverse a linked list given a pointer to the head node of the linked list.
Linked list reversal algorithm
Let head
be the first node of the linked list.
Iterative Algorithm
-
Initialize 3 pointers -
curr
set tohead
,prev
andnext
set toNULL
. -
Traverse the linked list until you reach the last node, which is
curr
!=NULL
and do the following:-
Set
next
tocurr->next 个
tonext
move to its next node. prev
Reverses the direction of the current node by pointing it toward . Therefore,curr->next
set toprev
.-
Set
prev
tocurr
to move it forward one position. -
Set
curr
tonext
to move it forward one position.
-
Set
Recursive Algorithm
-
Split the list into two parts: the first node, the
head
node, and the rest of the linked list. -
Call
reverse(head->next)
, i.e., the remainder of the reverse linked list, and store the reverse linked list asrev
. -
Append
head
to the end of the reverse-linked listrev
. -
Points to
head
, that is, the tail of the reverse link list points toNULL
Reverse Linked List using Stack
-
Initializes
head
a pointer to a linked listcurr
. - Traverse the linked list and insert each node one by one.
-
Update
head
to the last node in the linked list, which is the first node in the stack. - Start popping nodes from the stack one by one and appending it to the end of the reverse linked list.
-
Update the next pointer of the last node to
NULL
.
Linked list reversal diagram
-
Initialize
curr
to point to the head, that is, nodes2
and ,prev
andcurr
the data of isNULL
. -
Sets
next
to point tocurr->next
a value equal to4
. -
Set
curr->next
toprev
to obtain2
a list of backward links headed by . -
Move
prev
to the nodecurr
with data2
, andcurr
move to the nodenext
with data .4
-
Points
next
tocurr->next
a value equal to6
-
Set
curr->next
toprev
to obtain a reverse linked list with2
and4
as reverse nodes and with4
as the head. -
Move
prev
to , that is, the nodecurr
with data , and move to , that is, the node with data .4
curr
next
6
-
Sets
next
to point tocurr->next
a value equal to8
. -
Set
curr->next
toprev
to obtain a reverse linked list with and2
as reverse nodes and headed by .4
6
6
-
Move
prev
to , that is, the nodecurr
with data , and move to , that is, the node with data .6
curr
next
8
-
Point
next
tocurr->next
, that isNULL
. -
Set
curr->next
toprev
, with2
,4
,6
and8
as reverse nodes and with8
as the head to obtain a reverse linked list. -
Move
prev
tocurr
, that is,8
the node with data , movecurr
toNULL
, and the algorithm terminates.
Implementation of linked list reversal
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int x) {
this->data = x;
this->next = NULL;
}
};
void printList(Node* head)
{
Node*curr = head;
while (curr != NULL) {
cout << curr->data << " ";
curr = curr->next;
}
}
Node* reverse(Node* head)
{
Node* curr = head;
Node *prev = NULL, *next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;
return head;
}
Node* recursiveReverse(Node* head)
{
if (head == NULL || head->next == NULL)
return head;
Node* rest = recursiveReverse(head->next);
head->next->next = head;
head->next = NULL;
return rest;
}
void reverseLL(Node** head)
{
stack<Node*> s;
Node* temp = *head;
while (temp->next != NULL)
{
s.push(temp);
temp = temp->next;
}
*head = temp;
while (!s.empty())
{
temp->next = s.top();
s.pop();
temp = temp->next;
}
temp->next = NULL;
}
int main()
{
Node* head = new Node(1);
head -> next = new Node(2);
head -> next-> next = new Node(3);
head -> next-> next-> next = new Node(4);
head -> next-> next-> next-> next = new Node(5);
head -> next-> next-> next-> next-> next = new Node(6);
head = reverse(head);
printList(head); cout << "\n";
head = recursiveReverse(head);
printList(head); cout << "\n";
reverseLL(&head);
printList(head); cout << "\n";
return 0;
}
Linked list reversal algorithm complexity
Time Complexity
- Average situation
To reverse a complete linked list, we have to visit each node and then append it to the reversal list. Therefore, if a linked list has n
nodes, the average case time complexity of traversal is about O(n)
. The time complexity is about O(n)
.
- Best Case
The time complexity of the best case is O(n)
. It is the same as the time complexity of the average case.
- Worst case scenario
The worst case time complexity is O(n)
. It is the same as the best case time complexity.
Space complexity
The space complexity of this traversal algorithm is O(1)
, since no additional space is required except for the temporary pointers.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert a binary tree to a binary search tree
Publish Date:2025/03/18 Views:52 Category:ALGORITHM
-
A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left and right children. It can also be interpreted as an undirected graph where the topmost node
In-order descendants in a binary search tree
Publish Date:2025/03/18 Views:107 Category:ALGORITHM
-
The in-order descendant of a binary tree is the next node in the in-order traversal of the binary tree. So, for the last node in the tree, it is NULL . Since the in-order traversal of a binary search tree is a sorted array. The node with th
Binary Search Tree Deletion
Publish Date:2025/03/18 Views:93 Category:ALGORITHM
-
In the article Binary Search Trees: Searching and Inserting , we discussed how to insert an element into a binary search tree and how to search for a value in a binary search tree. In this article, we will discuss how to delete a node from
Binary Search Tree Check
Publish Date:2025/03/18 Views:79 Category:ALGORITHM
-
A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left children and right children. For a binary tree to be a BST, it must satisfy the following pr
Iterative insertion into a binary search tree
Publish Date:2025/03/18 Views:158 Category:ALGORITHM
-
In the previous article Binary Search Tree , we discussed the recursive method to insert a node in BST. In this article, we will discuss the iterative method to insert a node in BST. It is better than the recursive method because the iterat
Binary Search Tree
Publish Date:2025/03/18 Views:181 Category:ALGORITHM
-
A binary search tree (BST) is an ordered binary tree data structure based on nodes. A node has a value and two child nodes (a binary tree has at most two child nodes) connected to each other. A node has a value and two child nodes (a binary
Binary Tree Traversal
Publish Date:2025/03/18 Views:115 Category:ALGORITHM
-
A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left and right children. It can also be interpreted as an undirected graph where the topmost node
Circular Doubly Linked List
Publish Date:2025/03/18 Views:67 Category:ALGORITHM
-
A circular doubly linked list is a combination of a circular linked list and a doubly linked list. Its two nodes are connected by a previous and next pointer. The next pointer of the last node points to the first node, and the previous poin
Circular Linked List
Publish Date:2025/03/18 Views:172 Category:ALGORITHM
-
A circular linked list is a data structure that is slightly more complex than a linked list. It is a linked list where all the nodes are connected in a loop and form a chain. The last node does not have a NULL . Instead, it stores the addre