[Expected Approach - 1] Using Recursion - O(n) Time and O(n) Space:
To convert a singly linked list into a circular linked list using recursion, begin by recursively traversing the list to reach the last node, which is the node whose next pointer is nullptr. Update last node's next pointer to point back to the head node to form a circular link.
Below is the implementation of the above approach:
C++
// C++ Program for converting singly linked list// into circular linked list.#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};// Function that convert singly linked list// into circular linked list.voidcircular(Node*curr,Node*head){// if last node, then point next ptr// to head Nodeif(curr->next==nullptr){curr->next=head;return;}// otherwise traverse to the// next nodecircular(curr->next,head);}voidprintList(Node*head){Node*curr=head;do{cout<<curr->data<<" ";curr=curr->next;}while(curr!=head);cout<<endl;}intmain(){// create a hard coded list// 10->12->14->16Node*head=newNode(10);head->next=newNode(12);head->next->next=newNode(14);head->next->next->next=newNode(16);circular(head,head);printList(head);return0;}
C
// C Program for converting singly linked list// into circular linked list.#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};// Function that converts singly linked list into // circular linked listvoidcircular(structNode*curr,structNode*head){// if last node, then point next ptr// to head Nodeif(curr->next==NULL){curr->next=head;return;}// otherwise traverse to the // next nodecircular(curr->next,head);}voidprintList(structNode*head){structNode*curr=head;do{printf("%d ",curr->data);curr=curr->next;}while(curr!=head);printf("\n");}structNode*createNode(intdata){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=data;newNode->next=NULL;returnnewNode;}intmain(){// create a hard coded list 10->12->14->16structNode*head=createNode(10);head->next=createNode(12);head->next->next=createNode(14);head->next->next->next=createNode(16);circular(head,head);printList(head);return0;}
Java
// Java Program for converting singly linked list// into circular linked list.classNode{intdata;Nodenext;Node(intdata){this.data=data;this.next=null;}}publicclassGfG{// Function that converts singly linked // list into circular linked liststaticvoidcircular(Nodecurr,Nodehead){// if last node, then point next ptr// to head Nodeif(curr.next==null){curr.next=head;return;}// otherwise move to the // next nodecircular(curr.next,head);}staticvoidprintList(Nodehead){Nodecurr=head;do{System.out.print(curr.data+" ");curr=curr.next;}while(curr!=head);System.out.println();}publicstaticvoidmain(String[]args){// create a hard coded list 10->12->14->16Nodehead=newNode(10);head.next=newNode(12);head.next.next=newNode(14);head.next.next.next=newNode(16);circular(head,head);printList(head);}}
Python
# Python program for converting singly linked list# into circular linked list.classNode:def__init__(self,data):self.data=dataself.next=None# Function that converts singly linked list # into circular linked listdefcircular(curr,head):# if last node, then point next ptr# to head Nodeifcurr.next==None:curr.next=headreturn# otherwise move to the # next nodecircular(curr.next,head)defprintList(head):curr=headwhileTrue:print(curr.data,end=" ")curr=curr.nextifcurr==head:breakprint()if__name__=="__main__":# create a hard coded list 10->12->14->16head=Node(10)head.next=Node(12)head.next.next=Node(14)head.next.next.next=Node(16)circular(head,head)printList(head)
C#
// C# Program for converting singly linked list// into circular linked list.usingSystem;classNode{publicintdata;publicNodenext;publicNode(intdata){this.data=data;this.next=null;}}classGfG{// Function that converts singly linked list // into circular linked liststaticvoidCircular(Nodecurr,Nodehead){// if last node, then point next ptr// to head Nodeif(curr.next==null){curr.next=head;return;}// otherwise move to the // next nodeCircular(curr.next,head);}staticvoidPrintList(Nodehead){Nodecurr=head;do{Console.Write(curr.data+" ");curr=curr.next;}while(curr!=head);Console.WriteLine();}staticvoidMain(string[]args){// create a hard coded list 10->12->14->16Nodehead=newNode(10);head.next=newNode(12);head.next.next=newNode(14);head.next.next.next=newNode(16);Circular(head,head);PrintList(head);}}
JavaScript
// Javascript program for converting singly linked list// into circular linked list.classNode{constructor(data){this.data=data;this.next=null;}}// Function that converts singly linked // list into circular linked listfunctioncircular(curr,head){// if last node, then point next ptr// to head Nodeif(curr.next==null){curr.next=head;return;}// otherwise move to the // next nodecircular(curr.next,head);}functionprintList(head){letcurr=head;do{console.log(curr.data+" ");curr=curr.next;}while(curr!==head);}// create a hard coded list// 10->12->14->16lethead=newNode(10);head.next=newNode(12);head.next.next=newNode(14);head.next.next.next=newNode(16);circular(head,head);printList(head);
Output
10 12 14 16
Time Complexity: O(n), where n is the number of nodes in the list. Auxiliary Space: O(n)
[Expected Approach - 2] Using Iterative Approach - O(n) Time and O(1) Space:
To convert a singly linked list into a circular linked list iteratively, start by initializing a pointer to traverse the list from the head node. Traverse the list until we reach the node whose next pointer that is nullptr. Update its next pointer to point back to the head node.
Below is the implementation of the above approach:
C++
// C++ Program for converting singly linked list// into circular linked list.#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};// Function that convert singly linked list// into circular linked list.Node*circular(Node*head){// declare a node variable curr and// assign head node into curr node.Node*curr=head;// check that while curr->next is not equal// to NULL then curr points to next node.while(curr->next!=nullptr)curr=curr->next;// assign curr->next to the head node.curr->next=head;returnhead;}voidprintList(Node*head){Node*curr=head;do{cout<<curr->data<<" ";curr=curr->next;}while(curr!=head);cout<<endl;}intmain(){// create a hard coded list// 10->12->14->16Node*head=newNode(10);head->next=newNode(12);head->next->next=newNode(14);head->next->next->next=newNode(16);head=circular(head);printList(head);return0;}
C
// C Program for converting singly linked list// into circular linked list.#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};// Function that converts singly linked list into // circular linked liststructNode*circular(structNode*head){// declare a node variable curr and // assign head node into curr node.structNode*curr=head;// check that while curr->next is not equal// to NULL then curr points to next node.while(curr->next!=NULL)curr=curr->next;// assign curr->next to the head node.curr->next=head;returnhead;}voidprintList(structNode*head){structNode*curr=head;do{printf("%d ",curr->data);curr=curr->next;}while(curr!=head);printf("\n");}structNode*createNode(intdata){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=data;newNode->next=NULL;returnnewNode;}intmain(){// create a hard coded list 10->12->14->16structNode*head=createNode(10);head->next=createNode(12);head->next->next=createNode(14);head->next->next->next=createNode(16);head=circular(head);printList(head);return0;}
Java
// Java Program for converting singly linked list// into circular linked list.classNode{intdata;Nodenext;Node(intdata){this.data=data;this.next=null;}}publicclassGfG{// Function that converts singly linked // list into circular linked liststaticNodecircular(Nodehead){// declare a node variable curr and // assign head node into curr node.Nodecurr=head;// check that while curr->next is not equal// to NULL then curr points to next node.while(curr.next!=null)curr=curr.next;// assign curr->next to the head node.curr.next=head;returnhead;}staticvoidprintList(Nodehead){Nodecurr=head;do{System.out.print(curr.data+" ");curr=curr.next;}while(curr!=head);System.out.println();}publicstaticvoidmain(String[]args){// create a hard coded list 10->12->14->16Nodehead=newNode(10);head.next=newNode(12);head.next.next=newNode(14);head.next.next.next=newNode(16);head=circular(head);printList(head);}}
Python
# Python program for converting singly linked list# into circular linked list.classNode:def__init__(self,data):self.data=dataself.next=None# Function that converts singly linked list # into circular linked listdefcircular(head):# declare a node variable curr and # assign head node into curr node.curr=head# check that while curr->next is not equal# to NULL then curr points to next node.whilecurr.nextisnotNone:curr=curr.next# assign curr->next to the head node.curr.next=headreturnheaddefprintList(head):curr=headwhileTrue:print(curr.data,end=" ")curr=curr.nextifcurr==head:breakprint()if__name__=="__main__":# create a hard coded list 10->12->14->16head=Node(10)head.next=Node(12)head.next.next=Node(14)head.next.next.next=Node(16)head=circular(head)printList(head)
C#
// C# Program for converting singly linked list// into circular linked list.usingSystem;classNode{publicintdata;publicNodenext;publicNode(intdata){this.data=data;this.next=null;}}classGfG{// Function that converts singly linked list // into circular linked liststaticNodeCircular(Nodehead){// declare a node variable curr and // assign head node into curr node.Nodecurr=head;// check that while curr->next is not equal// to NULL then curr points to next node.while(curr.next!=null)curr=curr.next;// assign curr->next to the head node.curr.next=head;returnhead;}staticvoidPrintList(Nodehead){Nodecurr=head;do{Console.Write(curr.data+" ");curr=curr.next;}while(curr!=head);Console.WriteLine();}staticvoidMain(string[]args){// create a hard coded list 10->12->14->16Nodehead=newNode(10);head.next=newNode(12);head.next.next=newNode(14);head.next.next.next=newNode(16);head=Circular(head);PrintList(head);}}
JavaScript
// Javascript program for converting singly linked list// into circular linked list.classNode{constructor(data){this.data=data;this.next=null;}}// Function that converts singly linked // list into circular linked listfunctioncircular(head){// declare a node variable curr and // assign head node into curr node.letcurr=head;// check that while curr->next is not equal// to NULL then curr points to next node.while(curr.next!==null){curr=curr.next;}// assign curr->next to the head node.curr.next=head;returnhead;}functionprintList(head){letcurr=head;do{console.log(curr.data+" ");curr=curr.next;}while(curr!==head);}// create a hard coded list// 10->12->14->16lethead=newNode(10);head.next=newNode(12);head.next.next=newNode(14);head.next.next.next=newNode(16);head=circular(head);printList(head);
Output
10 12 14 16
Time Complexity: O(n), where n is the number of nodes in the list. Auxiliary Space: O(1)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.