Iteratively Reverse a linked list using only 2 pointers (An Interesting Method)
Last Updated :
11 Sep, 2023
Given pointer to the head node of a linked list, the task is to reverse the linked list.
Examples:
Input : Head of following linked list
1->2->3->4->NULL
Output : Linked list should be changed to,
4->3->2->1->NULL
Input : Head of following linked list
1->2->3->4->5->NULL
Output : Linked list should be changed to,
5->4->3->2->1->NULL
We have seen how to reverse a linked list in article Reverse a linked list. In iterative method we had used 3 pointers prev, cur and next. Below is an interesting approach that uses only two pointers. The idea is to use XOR to swap pointers.
C++
#include <bits/stdc++.h>
using namespace std;
typedef uintptr_t ut;
struct Node {
int data;
struct Node* next;
};
void reverse( struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
while (current != NULL) {
current
= ( struct Node*)((ut)prev ^ (ut)current
^ (ut)(current->next)
^ (ut)(current->next = prev)
^ (ut)(prev = current));
}
*head_ref = prev;
}
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;
}
void printList( struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
printf ( "%d " , temp->data);
temp = temp->next;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);
printf ( "Given linked list\n" );
printList(head);
reverse(&head);
printf ( "\nReversed Linked list \n" );
printList(head);
return 0;
}
|
Java
import java.util.*;
class Main {
static class Node {
int data;
Node next;
};
static Node head_ref = null ;
static void reverse()
{
Node prev = null ;
Node current = head_ref;
while (current != null ) {
Node next = current.next;
current.next = prev;
prev = current;
current = next;
}
head_ref = prev;
}
static void push( int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
}
static void printList()
{
Node temp = head_ref;
while (temp != null ) {
System.out.print(temp.data + " " );
temp = temp.next;
}
}
public static void main(String[] args)
{
push( 20 );
push( 4 );
push( 15 );
push( 85 );
System.out.print( "Given linked list\n" );
printList();
reverse();
System.out.print( "\nReversed Linked list \n" );
printList();
}
}
|
Python3
class node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
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
def push( self , new_data):
new_node = node(new_data)
new_node. next = self .head
self .head = new_node
def printList( self ):
temp = self .head
while (temp):
print (temp.data, end = " " )
temp = temp. next
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()
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
class Node {
public int data;
public Node next;
};
static Node head_ref = null ;
static void reverse()
{
Node prev = null ;
Node current = head_ref;
while (current != null ) {
Node next = current.next;
current.next = prev;
prev = current;
current = next;
}
head_ref = prev;
}
static void push( int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
}
static void printList()
{
Node temp = head_ref;
while (temp != null ) {
Console.Write(temp.data + " " );
temp = temp.next;
}
}
public static void Main( string [] args)
{
push(20);
push(4);
push(15);
push(85);
Console.Write( "Given linked list\n" );
printList();
reverse();
Console.Write( "\nReversed Linked list \n" );
printList();
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
}
var head_ref = null ;
function reverse()
{
var prev = null ;
var current = head_ref;
while (current != null )
{
var next = current.next;
current.next = prev;
prev = current;
current = next;
}
head_ref = prev;
}
function push(new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
}
function printList()
{
var temp = head_ref;
while (temp != null )
{
document.write(temp.data + " " );
temp = temp.next;
}
}
push(20);
push(4);
push(15);
push(85);
document.write( "Given linked list<br/>" );
printList();
reverse();
document.write( "<br/>Reversed Linked list <br/>" );
printList();
</script>
|
Output:
Given linked list
85 15 4 20
Reversed Linked list
20 4 15 85
Time Complexity: O(n)
Auxiliary Space: O(1) since using space for prev and next
Alternate Solution :
C++
#include <bits/stdc++.h>
using namespace std;
typedef uintptr_t ut;
struct Node {
int data;
struct Node* next;
};
void reverse( struct Node** head_ref)
{
struct Node* current = *head_ref;
struct Node* next;
while (current->next != NULL) {
next = current->next;
current->next = next->next;
next->next = (*head_ref);
*head_ref = 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;
}
void printList( struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
printf ( "%d " , temp->data);
temp = temp->next;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);
printf ( "Given linked list\n" );
printList(head);
reverse(&head);
printf ( "\nReversed Linked list \n" );
printList(head);
return 0;
}
|
Java
import java.util.*;
class Main {
static class Node {
int data;
Node next;
};
static Node head_ref = null ;
static void reverse()
{
Node next;
Node curr = head_ref;
while (curr.next != null ) {
next = curr.next;
curr.next = next.next;
next.next = head_ref;
head_ref = next;
}
}
static void push( int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
}
static void printList()
{
Node temp = head_ref;
while (temp != null ) {
System.out.print(temp.data + " " );
temp = temp.next;
}
}
public static void main(String[] args)
{
push( 20 );
push( 4 );
push( 15 );
push( 85 );
System.out.print( "Given linked list\n" );
printList();
reverse();
System.out.print( "\nReversed Linked list \n" );
printList();
}
}
|
Python3
class Node :
def __init__( self ):
self .data = 0
self . next = None
def reverse(head_ref):
current = head_ref
next = None
while (current. next ! = None ) :
next = current. next
current. next = next . next
next . next = (head_ref)
head_ref = next
return head_ref
def push( head_ref, new_data):
new_node = Node()
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
def printList( head):
temp = head
while (temp ! = None ) :
print ( temp.data, end = " " )
temp = temp. next
head = None
head = push(head, 20 )
head = push(head, 4 )
head = push(head, 15 )
head = push(head, 85 )
print ( "Given linked list" )
printList(head)
head = reverse(head)
print ( "\nReversed Linked list " )
printList(head)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
class Node {
public int data;
public Node next;
};
static Node head_ref = null ;
static void reverse()
{
Node current = head_ref;
Node next;
while (current.next != null ) {
next = current.next;
current.next = next.next;
next.next = head_ref;
head_ref = next;
}
}
static void push( int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
}
static void printList()
{
Node temp = head_ref;
while (temp != null ) {
Console.Write(temp.data + " " );
temp = temp.next;
}
}
public static void Main( string [] args)
{
push(20);
push(4);
push(15);
push(85);
Console.Write( "Given linked list\n" );
printList();
reverse();
Console.Write( "\nReversed Linked list \n" );
printList();
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
}
var head_ref = null ;
function reverse()
{
var current = head_ref;
var next;
while (current.next != null )
{
next = current.next;
current.next = next.next;
next.next = head_ref;
head_ref = next;
}
}
function push(new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
}
function printList()
{
var temp = head_ref;
while (temp != null )
{
document.write(temp.data + " " );
temp = temp.next;
}
}
push(20);
push(4);
push(15);
push(85);
document.write( "Given linked list<br/>" );
printList();
reverse();
document.write( "<br/>Reversed Linked list <br/>" );
printList();
</script>
|
Output:
Given linked list
85 15 4 20
Reversed Linked list
20 4 15 85
Time Complexity : O(n)
Auxiliary Space: O(1)
Thanks to Abhay Yadav for suggesting this approach.
Share your thoughts in the comments
Please Login to comment...