Convert singly linked list into circular linked list
Last Updated :
24 May, 2023
Given a singly linked list, we have to convert it into circular linked list. For example, we have been given a singly linked list with four nodes and we want to convert this singly linked list into circular linked list.
The above singly linked list is converted into circular linked list.
Approach: The idea is to traverse the singly linked list and check if the node is the last node or not. If the node is the last node i.e pointing to NULL then make it point to the starting node i.e head node. Below is the implementation of this approach.
Algorithm:
Here are the algorithmic steps to convert a singly linked list into a circular linked list:
Step 1 : Initialize a pointer current to the head node of the singly linked list.
Step 2 : Traverse the linked list by moving the current pointer to the next node until it
reaches the last node (i.e., the node whose next pointer is NULL).
Step 3 : Set the next pointer of the last node to point back to the head node
of the linked list.
The singly linked list is now a circular linked list.
Note that step 3 is what actually converts the singly linked list into a
circular linked list.
Implementation:
C++
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
struct Node* circular( struct Node* head)
{
struct Node* start = head;
while (head->next != NULL)
head = head->next;
head->next = start;
return start;
}
void push( struct Node** head, int data)
{
struct Node* newNode = ( struct Node*) malloc
( sizeof ( struct Node));
newNode->data = data;
newNode->next = (*head);
(*head) = newNode;
}
void displayList( struct Node* node)
{
struct Node* start = node;
while (node->next != start) {
cout << " " << node->data;
node = node->next;
}
cout << " " << node->data;
}
int main()
{
struct Node* head = NULL;
push(&head, 15);
push(&head, 14);
push(&head, 13);
push(&head, 22);
push(&head, 17);
circular(head);
cout << "Display list: \n" ;
displayList(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* circular( struct Node* head){
struct Node* start=head;
while (head->next != NULL){
head = head->next;
}
head->next = start;
return start;
}
void push( struct Node** head, int data)
{
struct Node* newNode = ( struct Node*) malloc ( sizeof ( struct Node));
newNode->data = data;
newNode->next = (*head);
(*head) = newNode;
}
void displayList( struct Node* node)
{
struct Node* start = node;
while (node->next != start) {
printf ( "%d " , node->data);
node = node->next;
}
printf ( "%d " , node->data);
}
int main()
{
struct Node* head = NULL;
push(&head, 15);
push(&head, 14);
push(&head, 13);
push(&head, 22);
push(&head, 17);
circular(head);
printf ( "Display list: \n" );
displayList(head);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
public class GFG
{
static class Node
{
int data;
Node next;
};
static Node circular(Node head)
{
Node start = head;
while (head.next != null )
head = head.next;
head.next = start;
return start;
}
static Node push(Node head, int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = (head);
(head) = newNode;
return head;
}
static void displayList( Node node)
{
Node start = node;
while (node.next != start)
{
System.out.print( " " + node.data);
node = node.next;
}
System.out.print( " " + node.data);
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 15 );
head = push(head, 14 );
head = push(head, 13 );
head = push(head, 22 );
head = push(head, 17 );
circular(head);
System.out.print( "Display list: \n" );
displayList(head);
}
}
|
Python3
import sys
class Node:
def __init__( self ,data):
self .data = data
self . next = None
def push(head, data):
if not head:
return Node(data)
newNode = Node(data)
newNode. next = head
head = newNode
return head
def circular(head):
start = head
while (head. next is not None ):
head = head. next
head. next = start
return start
def displayList(node):
start = node
while (node. next is not start):
print ( "{} " . format (node.data),end = "")
node = node. next
print ( "{} " . format (node.data),end = "")
if __name__ = = '__main__' :
head = None
head = push(head, 15 )
head = push(head, 14 )
head = push(head, 13 )
head = push(head, 22 )
head = push(head, 17 )
circular(head)
print ( "Display List:" )
displayList(head)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node circular(Node head)
{
Node start = head;
while (head.next != null )
head = head.next;
head.next = start;
return start;
}
static Node push(Node head, int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = (head);
(head) = newNode;
return head;
}
static void displayList( Node node)
{
Node start = node;
while (node.next != start)
{
Console.Write( " " + node.data);
node = node.next;
}
Console.Write( " " + node.data);
}
public static void Main(String []args)
{
Node head = null ;
head = push(head, 15);
head = push(head, 14);
head = push(head, 13);
head = push(head, 22);
head = push(head, 17);
circular(head);
Console.Write( "Display list: \n" );
displayList(head);
}
}
|
Javascript
<script>
class Node
{
constructor(val)
{
this .data = val;
this .next = null ;
}
}
function circular( head)
{
start = head;
while (head.next != null )
head = head.next;
head.next = start;
return start;
}
function push( head , data)
{
newNode = new Node();
newNode.data = data;
newNode.next = (head);
(head) = newNode;
return head;
}
function displayList( node)
{
start = node;
while (node.next != start)
{
document.write( " " + node.data);
node = node.next;
}
document.write( " " + node.data);
}
head = null ;
head = push(head, 15);
head = push(head, 14);
head = push(head, 13);
head = push(head, 22);
head = push(head, 17);
circular(head);
document.write( "Display list: <br/>" );
displayList(head);
</script>
|
Output
Display list:
17 22 13 14 15
Time Complexity: O(n), As we need to move through the whole list to get hold of the last node.
Auxiliary Space: O(1), As constant extra space is used.
Example:
Here’s a C++ implementation of a function that converts a singly linked list into a circular linked list with minimum time complexity:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node( int value) {
data = value;
next = NULL;
}
};
void convertToCircular(Node* head) {
Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = head;
}
void printList(Node* head) {
Node* current = head;
do {
cout << current->data << " " ;
current = current->next;
} while (current != head);
}
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);
convertToCircular(head);
cout << "The circular linked list is: " ;
printList(head);
return 0;
}
|
Java
import java.io.*;
public class Node {
public static class NodeClass {
int data;
NodeClass next;
public NodeClass( int value)
{
data = value;
next = null ;
}
}
public static void convertToCircular(NodeClass head)
{
NodeClass current = head;
while (current.next != null ) {
current = current.next;
}
current.next = head;
}
public static void printList(NodeClass head)
{
NodeClass current = head;
do {
System.out.print(current.data + " " );
current = current.next;
} while (current != head);
}
public static void main(String[] args)
{
NodeClass head = new NodeClass( 1 );
head.next = new NodeClass( 2 );
head.next.next = new NodeClass( 3 );
head.next.next.next = new NodeClass( 4 );
convertToCircular(head);
System.out.println( "The circular linked list is: " );
printList(head);
}
}
|
Python3
class Node:
def __init__( self , value):
self .data = value
self . next = None
def convertToCircular(head):
current = head
while current. next ! = None :
current = current. next
current. next = head
def printList(head):
current = head
while True :
print (current.data, end = " " )
current = current. next
if current = = head:
break
if __name__ = = "__main__" :
head = Node( 1 )
head. next = Node( 2 )
head. next . next = Node( 3 )
head. next . next . next = Node( 4 )
convertToCircular(head)
print ( "The circular linked list is:" )
printList(head)
|
C#
using System;
public class Node {
public int data;
public Node next;
public Node( int value) {
data = value;
next = null ;
}
}
public class LinkedList {
public static void ConvertToCircular(Node head) {
Node current = head;
while (current.next != null ) {
current = current.next;
}
current.next = head;
}
public static void PrintList(Node head) {
Node current = head;
do {
Console.Write(current.data + " " );
current = current.next;
} while (current != head);
}
}
class Program {
static void Main( string [] args) {
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
LinkedList.ConvertToCircular(head);
Console.Write( "The circular linked list is: " );
LinkedList.PrintList(head);
}
}
|
Javascript
class Node {
constructor(value) {
this .data = value;
this .next = null ;
}
}
function convertToCircular(head) {
let current = head;
while (current.next !== null ) {
current = current.next;
}
current.next = head;
}
function printList(head) {
let current = head;
do {
console.log(current.data + " " );
current = current.next;
} while (current !== head);
}
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
convertToCircular(head);
console.log( "The circular linked list is: " );
printList(head);
|
Output
The circular linked list is: 1 2 3 4
Time Complexity: O(n), It is O(n) because we are traversing the whole the linked list so it requires n iteration.
Auxiliary Space: O(1), as here only constant space is used but if for general linked list it is O(n). Hence it is O(1).
Explanation:
In this implementation, the convertToCircular() function takes a pointer to the head node of a singly linked list as input and modifies the last node of the linked list to point back to the head node, effectively converting the singly linked list into a circular linked list. The function does this by initializing a current pointer to the head node, and then traversing the linked list until it reaches the last node. It then sets the next pointer of the last node to the head node, creating a circular link.
The printList() function is used to print the values of the nodes in the circular linked list. It does this by initializing a current pointer to the head node, and then traversing the linked list using a do-while loop that continues until the current pointer reaches the head node again. This ensures that all nodes in the circular linked list are printed.
The main() function creates a sample singly linked list and calls the convertToCircular() function to convert it to a circular linked list. It then calls the printList() function to print the values of the nodes in the circular linked list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...