Print reverse of a Linked List without extra space and modifications
Last Updated :
29 Mar, 2024
Given a Linked List, display the linked list in reverse without using recursion, stack or modifications to given list.
Examples:
Input: 1->2->3->4->5->NULL
Output: 5 4 3 2 1
Input: 10->5->15->20->24->NULL
Output: 24 20 15 5 10
Below are different solutions that are now allowed here as we cannot use extra space and modify list.
1) Recursive solution to print reverse a linked list. Requires extra space.
2) Reverse linked list and then print. This requires modifications to original list.
3) Stack based solution to print linked list reverse. Push all nodes one by one to a stack. Then one by one pop elements from stack and print. This also requires extra space.
Algorithms:
1) Find n = count nodes in linked list.
2) For i = n to 1, do following.
Print i-th node using get n-th node function
C++
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node =
( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int getCount( struct Node* head)
{
int count = 0;
struct Node* current = head;
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
int getNth( struct Node* head, int n)
{
struct Node* curr = head;
for ( int i=0; i<n-1 && curr != NULL; i++)
curr = curr->next;
return curr->data;
}
void printReverse(Node *head)
{
int n = getCount(head);
for ( int i=n; i>=1; i--)
printf ( "%d " , getNth(head, i));
}
int main()
{
struct Node* head = NULL;
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
printReverse(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node head;
static void push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
head = head_ref;
}
static int getCount(Node head)
{
int count = 0 ;
Node current = head;
while (current != null )
{
count++;
current = current.next;
}
return count;
}
static int getNth(Node head, int n)
{
Node curr = head;
for ( int i = 0 ; i < n - 1 && curr != null ; i++)
curr = curr.next;
return curr.data;
}
static void printReverse()
{
int n = getCount(head);
for ( int i = n; i >= 1 ; i--)
System.out.printf( "%d " , getNth(head, i));
}
public static void main(String[] args)
{
head = null ;
push(head, 5 );
push(head, 4 );
push(head, 3 );
push(head, 2 );
push(head, 1 );
printReverse();
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push( head_ref, new_data):
new_node = Node(new_data)
new_node. next = head_ref;
head_ref = new_node;
return head_ref
def getCount(head):
count = 0 ;
current = head;
while (current ! = None ):
count + = 1
current = current. next ;
return count;
def getNth(head, n):
curr = head;
i = 0
while i < n - 1 and curr ! = None :
curr = curr. next ;
i + = 1
return curr.data;
def printReverse(head):
n = getCount(head);
for i in range (n, 0 , - 1 ):
print (getNth(head, i), end = ' ' );
if __name__ = = '__main__' :
head = None ;
head = push(head, 5 );
head = push(head, 4 );
head = push(head, 3 );
head = push(head, 2 );
head = push(head, 1 );
printReverse(head);
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node head;
static void push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
head = head_ref;
}
static int getCount(Node head)
{
int count = 0;
Node current = head;
while (current != null )
{
count++;
current = current.next;
}
return count;
}
static int getNth(Node head, int n)
{
Node curr = head;
for ( int i = 0;
i < n - 1 && curr != null ; i++)
curr = curr.next;
return curr.data;
}
static void printReverse()
{
int n = getCount(head);
for ( int i = n; i >= 1; i--)
Console.Write( "{0} " , getNth(head, i));
}
public static void Main(String[] args)
{
head = null ;
push(head, 5);
push(head, 4);
push(head, 3);
push(head, 2);
push(head, 1);
printReverse();
}
}
|
Javascript
<script>
class Node{
constructor(data){
this .data = data
this .next = null
}
}
function push( head_ref, new_data){
let new_node = new Node(new_data)
new_node.next = head_ref;
head_ref = new_node;
return head_ref
}
function getCount(head){
let count = 0;
let current = head;
while (current != null ){
count += 1
current = current.next;
}
return count;
}
function getNth(head, n){
let curr = head;
let i = 0
while (i < n - 1 && curr != null ){
curr = curr.next;
i += 1
}
return curr.data;
}
function printReverse(head){
let n = getCount(head);
for (let i=n;i>0;i--)
document.write(getNth(head, i), ' ' );
}
let head = null ;
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
printReverse(head);
</script>
|
Output:
5 4 3 2 1
Time complexity: O(n2) where n is the number of nodes in the given linked list
Auxiliary Space: O(1), as constant extra space is required.
Share your thoughts in the comments
Please Login to comment...