Given a linked list. The task is to reverse the order of the elements of the Linked List using an auxiliary Stack.
Examples:
Input : List = 3 -> 2 -> 1
Output : 1 -> 2 -> 3
Input : 9 -> 7 -> 4 -> 2
Output : 2 -> 4 -> 7 -> 9
Algorithm:
- Traverse the list and push all of its nodes onto a stack.
- Traverse the list from the head node again and pop a value from the stack top and connect them in reverse order.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
Node *reverseList(Node* head)
{
stack<Node *> stk;
Node* ptr = head;
while (ptr->next != NULL) {
stk.push(ptr);
ptr = ptr->next;
}
head = ptr;
while (!stk.empty()) {
ptr->next = stk.top();
ptr = ptr->next;
stk.pop();
}
ptr->next = NULL;
return head;
}
void printList(Node* head)
{
while (head) {
cout << head->data << " " ;
head = head->next;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
head = reverseList(head);
printList(head);
return 0;
}
|
Java
import java.util.*;
class GfG
{
static class Node
{
int data;
Node next;
}
static Node head = null ;
static void push( int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head);
(head) = new_node;
}
static Node reverseList(Node head)
{
Stack<Node > stk = new Stack<Node> ();
Node ptr = head;
while (ptr.next != null )
{
stk.push(ptr);
ptr = ptr.next;
}
head = ptr;
while (!stk.isEmpty())
{
ptr.next = stk.peek();
ptr = ptr.next;
stk.pop();
}
ptr.next = null ;
return head;
}
static void printList(Node head)
{
while (head != null )
{
System.out.print(head.data + " " );
head = head.next;
}
}
public static void main(String[] args)
{
push( 5 );
push( 4 );
push( 3 );
push( 2 );
push( 1 );
head = reverseList(head);
printList(head);
}
}
|
Python3
class Node:
def __init__( self , data, next ):
self .data = data
self . next = next
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data, self .head)
self .head = new_node
def reverseList( self ):
stk = []
ptr = self .head
while ptr. next ! = None :
stk.append(ptr)
ptr = ptr. next
self .head = ptr
while len (stk) ! = 0 :
ptr. next = stk.pop()
ptr = ptr. next
ptr. next = None
def printList( self ):
curr = self .head
while curr:
print (curr.data, end = " " )
curr = curr. next
if __name__ = = "__main__" :
linkedList = LinkedList()
linkedList.push( 5 )
linkedList.push( 4 )
linkedList.push( 3 )
linkedList.push( 2 )
linkedList.push( 1 )
linkedList.reverseList()
linkedList.printList()
|
C#
using System;
using System.Collections.Generic;
class GfG
{
public class Node
{
public int data;
public Node next;
}
static Node head = null ;
static void push( int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head);
(head) = new_node;
}
static Node reverseList(Node head)
{
Stack<Node > stk = new Stack<Node> ();
Node ptr = head;
while (ptr.next != null )
{
stk.Push(ptr);
ptr = ptr.next;
}
head = ptr;
while (stk.Count != 0)
{
ptr.next = stk.Peek();
ptr = ptr.next;
stk.Pop();
}
ptr.next = null ;
return head;
}
static void printList(Node head)
{
while (head != null )
{
Console.Write(head.data + " " );
head = head.next;
}
}
public static void Main(String[] args)
{
push( 5);
push( 4);
push( 3);
push( 2);
push( 1);
head = reverseList(head);
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
var head = null ;
function push(new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head;
head = new_node;
}
function reverseList(head) {
var stk = [];
var ptr = head;
while (ptr.next != null ) {
stk.push(ptr);
ptr = ptr.next;
}
head = ptr;
while (stk.length != 0) {
ptr.next = stk[stk.length - 1];
ptr = ptr.next;
stk.pop();
}
ptr.next = null ;
return head;
}
function printList(head) {
while (head != null ) {
document.write(head.data + " " );
head = head.next;
}
}
push(5);
push(4);
push(3);
push(2);
push(1);
head = reverseList(head);
printList(head);
</script>
|
Complexity Analysis:
- Time Complexity : O(n), as we are traversing over the linked list of size N using a while loop.
- Auxiliary Space: O(N), as we are using stack of size N in worst case which is extra space.
We can reverse a linked list with O(1) auxiliary space. See more methods to reverse a linked list.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Jan, 2023
Like Article
Save Article