Reverse a circular linked list
Last Updated :
24 Oct, 2022
Given a circular linked list of size n. The problem is to reverse the given circular linked list by changing links between the nodes.
Examples:
INPUT:
OUTPUT:
Approach: The approach is same as followed in reversing a singly linked list. Only here we have to make one more adjustment by linking the last node of the reversed list to the first node.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode( int data)
{
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void reverse(Node** head_ref)
{
if (*head_ref == NULL)
return ;
Node* prev = NULL;
Node* current = *head_ref;
Node* next;
do {
next = current->next;
current->next = prev;
prev = current;
current = next;
} while (current != (*head_ref));
(*head_ref)->next = prev;
*head_ref = prev;
}
void printList(Node* head)
{
if (head == NULL)
return ;
Node* temp = head;
do {
cout << temp->data << " " ;
temp = temp->next;
} while (temp != head);
}
int main()
{
Node* head = getNode(1);
head->next = getNode(2);
head->next->next = getNode(3);
head->next->next->next = getNode(4);
head->next->next->next->next = head;
cout << "Given circular linked list: " ;
printList(head);
reverse(&head);
cout << "\nReversed circular linked list: " ;
printList(head);
return 0;
}
|
Java
class GFG {
static class Node {
int data;
Node next;
};
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static Node reverse(Node head_ref)
{
if (head_ref == null )
return null ;
Node prev = null ;
Node current = head_ref;
Node next;
do {
next = current.next;
current.next = prev;
prev = current;
current = next;
} while (current != (head_ref));
(head_ref).next = prev;
head_ref = prev;
return head_ref;
}
static void printList(Node head)
{
if (head == null )
return ;
Node temp = head;
do {
System.out.print(temp.data + " " );
temp = temp.next;
} while (temp != head);
}
public static void main(String args[])
{
Node head = getNode( 1 );
head.next = getNode( 2 );
head.next.next = getNode( 3 );
head.next.next.next = getNode( 4 );
head.next.next.next.next = head;
System.out.print( "Given circular linked list: " );
printList(head);
head = reverse(head);
System.out.print(
"\nReversed circular linked list: " );
printList(head);
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def getNode(data):
newNode = Node(data)
newNode.data = data
newNode. next = None
return newNode
def reverse(head_ref):
if (head_ref = = None ):
return None
prev = None
current = head_ref
next = current. next
current. next = prev
prev = current
current = next
while (current ! = head_ref):
next = current. next
current. next = prev
prev = current
current = next
head_ref. next = prev
head_ref = prev
return head_ref
def printList(head):
if (head = = None ):
return
temp = head
print (temp.data, end = " " )
temp = temp. next
while (temp ! = head):
print (temp.data, end = " " )
temp = temp. next
if __name__ = = '__main__' :
head = getNode( 1 )
head. next = getNode( 2 )
head. next . next = getNode( 3 )
head. next . next . next = getNode( 4 )
head. next . next . next . next = head
print ( "Given circular linked list: " ,
end = "")
printList(head)
head = reverse(head)
print ( "\nReversed circular linked list: " ,
end = "")
printList(head)
|
C#
using System;
class GFG {
public class Node {
public int data;
public Node next;
};
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static Node reverse(Node head_ref)
{
if (head_ref == null )
return null ;
Node prev = null ;
Node current = head_ref;
Node next;
do {
next = current.next;
current.next = prev;
prev = current;
current = next;
} while (current != (head_ref));
(head_ref).next = prev;
head_ref = prev;
return head_ref;
}
static void printList(Node head)
{
if (head == null )
return ;
Node temp = head;
do {
Console.Write(temp.data + " " );
temp = temp.next;
} while (temp != head);
}
public static void Main(String[] args)
{
Node head = getNode(1);
head.next = getNode(2);
head.next.next = getNode(3);
head.next.next.next = getNode(4);
head.next.next.next.next = head;
Console.Write( "Given circular linked list: " );
printList(head);
head = reverse(head);
Console.Write( "\nReversed circular linked list: " );
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function getNode(data) {
var newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
function reverse(head_ref) {
if (head_ref == null ) return null ;
var prev = null ;
var current = head_ref;
var next;
do {
next = current.next;
current.next = prev;
prev = current;
current = next;
} while (current != head_ref);
head_ref.next = prev;
head_ref = prev;
return head_ref;
}
function printList(head) {
if (head == null ) return ;
var temp = head;
do {
document.write(temp.data + " " );
temp = temp.next;
} while (temp != head);
}
var head = getNode(1);
head.next = getNode(2);
head.next.next = getNode(3);
head.next.next.next = getNode(4);
head.next.next.next.next = head;
document.write( "Given circular linked list: " );
printList(head);
head = reverse(head);
document.write( "<br>Reversed circular linked list: " );
printList(head);
</script>
|
Output
Given circular linked list: 1 2 3 4
Reversed circular linked list: 4 3 2 1
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...