[Expected Approach 1] Using Recursion - O(n) Time and O(n) Space:
To idea is to traverse a circular linked list recursively, we will start by printing the value of the current node. Then, recursively call the function to handle the next node. If the next node is the same as the head node, indicating that a full cycle has been completed, we will end the recursion call.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};voidprintList(Node*curr,Node*head){// return if list is emptyif(head==nullptr)return;cout<<curr->data<<" ";if(curr->next==head)return;printList(curr->next,head);}intmain(){// Create a hard-coded linked list// 11 -> 2 -> 56 -> 12Node*head=newNode(11);head->next=newNode(2);head->next->next=newNode(56);head->next->next->next=newNode(12);head->next->next->next->next=head;printList(head,head);return0;}
C
#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};voidprintList(structNode*curr,structNode*head){// return if list is emptyif(head==NULL)return;printf("%d ",curr->data);if(curr->next==head)return;printList(curr->next,head);}structNode*createNode(intdata){structNode*new_node=(structNode*)malloc(sizeof(structNode));new_node->data=data;new_node->next=NULL;returnnew_node;}intmain(){// Create a hard-coded linked list// 11 -> 2 -> 56 -> 12structNode*head=createNode(11);head->next=createNode(2);head->next->next=createNode(56);head->next->next->next=createNode(12);head->next->next->next->next=head;printList(head,head);return0;}
Java
classNode{intdata;Nodenext;Node(intdata){this.data=data;this.next=null;}}publicclassGfG{staticvoidprintList(Nodecurr,Nodehead){// return if list is emptyif(head==null)return;System.out.print(curr.data+" ");if(curr.next==head)return;printList(curr.next,head);}publicstaticvoidmain(String[]args){// Create a hard-coded linked list// 11 -> 2 -> 56 -> 12Nodehead=newNode(11);head.next=newNode(2);head.next.next=newNode(56);head.next.next.next=newNode(12);head.next.next.next.next=head;printList(head,head);}}
Python
classNode:def__init__(self,data):self.data=dataself.next=Nonedefprint_list(curr,head):# return if list is emptyifheadisNone:returnprint(curr.data,end=" ")if(curr.next==head):returnprint_list(curr.next,head)if__name__=="__main__":# Create a hard-coded linked list# 11 -> 2 -> 56 -> 12head=Node(11)head.next=Node(2)head.next.next=Node(56)head.next.next.next=Node(12)head.next.next.next.next=headprint_list(head,head)
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intnewData){data=newData;next=null;}}classGfG{staticvoidPrintList(Nodecurr,Nodehead){// return if list is emptyif(head==null)return;Console.Write(curr.data+" ");if(curr.next==head)return;PrintList(curr.next,head);}staticvoidMain(string[]args){// Create a hard-coded linked list// 11 -> 2 -> 56 -> 12Nodehead=newNode(11);head.next=newNode(2);head.next.next=newNode(56);head.next.next.next=newNode(12);head.next.next.next.next=head;PrintList(head,head);}}
JavaScript
classNode{constructor(data){this.data=data;this.next=null;}}functionprintList(curr,head){// return if list is emptyif(head===null)return;console.log(curr.data);if(curr.next==head)return;printList(curr.next,head);}// Create a hard-coded linked list// 11 -> 2 -> 56 -> 12lethead=newNode(11);head.next=newNode(2);head.next.next=newNode(56);head.next.next.next=newNode(12);head.next.next.next.next=head;printList(head,head);
Output
11 2 56 12
[Expected Approach 2] Using Iterative Method - O(n) Time and O(1) Space:
To idea is to traverse a circular linked list iteratively, starting at the head node and repeatedly printing the value of the current node while moving to its next node. Continue this process until we return back to the head node, indicating that the full cycle of the circular linked list has been completed.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};voidprintList(Node*head){// return if list is emptyif(head==nullptr)return;Node*curr=head;do{cout<<curr->data<<" ";curr=curr->next;}while(curr!=head);cout<<endl;}intmain(){// Create a hard-coded linked list// 11 -> 2 -> 56 -> 12Node*head=newNode(11);head->next=newNode(2);head->next->next=newNode(56);head->next->next->next=newNode(12);head->next->next->next->next=head;printList(head);return0;}
C
#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};voidprintList(structNode*head){// return if list is emptyif(head==NULL)return;structNode*curr=head;do{printf("%d ",curr->data);curr=curr->next;}while(curr!=head);printf("\n");}structNode*createNode(intdata){structNode*new_node=(structNode*)malloc(sizeof(structNode));new_node->data=data;new_node->next=NULL;returnnew_node;}intmain(){// Create a hard-coded linked list// 11 -> 2 -> 56 -> 12structNode*head=createNode(11);head->next=createNode(2);head->next->next=createNode(56);head->next->next->next=createNode(12);head->next->next->next->next=head;printList(head);return0;}
Java
classNode{intdata;Nodenext;Node(intdata){this.data=data;this.next=null;}}publicclassGfG{staticvoidprintList(Nodehead){// return if list is emptyif(head==null)return;Nodecurr=head;do{System.out.print(curr.data+" ");curr=curr.next;}while(curr!=head);System.out.println();}publicstaticvoidmain(String[]args){// Create a hard-coded linked list// 11 -> 2 -> 56 -> 12Nodehead=newNode(11);head.next=newNode(2);head.next.next=newNode(56);head.next.next.next=newNode(12);head.next.next.next.next=head;printList(head);}}
Python
classNode:def__init__(self,data):self.data=dataself.next=Nonedefprint_list(head):# return if list is emptyifheadisNone:returncurr=headwhileTrue:print(curr.data,end=" ")curr=curr.nextifcurr==head:breakprint()if__name__=="__main__":# Create a hard-coded linked list# 11 -> 2 -> 56 -> 12head=Node(11)head.next=Node(2)head.next.next=Node(56)head.next.next.next=Node(12)head.next.next.next.next=headprint_list(head)
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intnewData){data=newData;next=null;}}classGfG{staticvoidPrintList(Nodehead){// return if list is emptyif(head==null)return;Nodecurr=head;do{Console.Write(curr.data+" ");curr=curr.next;}while(curr!=head);Console.WriteLine();}staticvoidMain(string[]args){// Create a hard-coded linked list//11 -> 2 -> 56 -> 12Nodehead=newNode(11);head.next=newNode(2);head.next.next=newNode(56);head.next.next.next=newNode(12);head.next.next.next.next=head;PrintList(head);}}
JavaScript
classNode{constructor(data){this.data=data;this.next=null;}}functionprintList(head){// return if list is emptyif(head===null)return;letcurr=head;do{console.log(curr.data+" ");curr=curr.next;}while(curr!==head);console.log();}// Create a hard-coded linked list// 11 -> 2 -> 56 -> 12lethead=newNode(11);head.next=newNode(2);head.next.next=newNode(56);head.next.next.next=newNode(12);head.next.next.next.next=head;printList(head);