Given the head of a circular linked list, print the data of the nodes in the linked list starting from the head node, traversing the list exactly once.
Example:
Input:
Output: 1 7 8 10 Explanation: The traversal begins at the head node 1, then subsequent nodes 7, 8, and 10.
Input:
Output: 2 5 7 8 10 Explanation: The traversal begins at head node 2, then subsequent nodes 5, 7, 8, and 10.
[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;}};voidprint(Node*curr,Node*head){// return if list is emptyif(head==nullptr)return;cout<<curr->data<<" ";if(curr->next==head)return;print(curr->next,head);}voidprintList(Node*head){print(head,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);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{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}publicclassGFG{publicstaticvoidprint(Nodecurr,Nodehead){// return if list is emptyif(head==null)return;System.out.print(curr.data+" ");if(curr.next==head)return;print(curr.next,head);}publicstaticvoidprintList(Nodehead){print(head,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);}}
Python
classNode:def__init__(self,x):self.data=xself.next=NonedefprintRec(curr,head):# return if list is emptyifheadisNone:returnprint(curr.data,end=' ')ifcurr.next==head:returnprintRec(curr.next,head)defprintList(head):printRec(head,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=headprintList(head)
C#
usingSystem;publicclassNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}publicclassGFG{publicstaticvoidprint(Nodecurr,Nodehead){// return if list is emptyif(head==null)return;Console.Write(curr.data+" ");if(curr.next==head)return;print(curr.next,head);}publicstaticvoidprintList(Nodehead){print(head,head);}publicstaticvoidMain(){// 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(x){this.data=x;this.next=null;}}functionprintRec(curr,head){// return if list is emptyif(head===null)return;process.stdout.write(curr.data+' ');if(curr.next===head)return;printRec(curr.next,head);}functionprintList(head){printRec(head,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);
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;// initialize current node as headNode*curr=head;// loop through the circular linked listdo{// print the data of current nodecout<<curr->data<<" ";// move to the next nodecurr=curr->next;}while(curr!=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);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
importjava.io.*;classNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}publicclassGFG{publicstaticvoidprintList(Nodehead){// return if list is emptyif(head==null)return;// initialize current node as headNodecurr=head;// loop through the circular linked listdo{// print the data of current nodeSystem.out.print(curr.data+" ");// move to the next nodecurr=curr.next;}while(curr!=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);}}
Python
classNode:def__init__(self,x):self.data=xself.next=NonedefprintList(head):# return if list is emptyifheadisNone:return# initialize current node as headcurr=head# loop through the circular linked listwhileTrue:# print the data of current nodeprint(curr.data,end=' ')# move to the next nodecurr=curr.nextifcurr==head:breakif__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=headprintList(head)
C#
usingSystem;publicclassNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}publicclassGFG{publicstaticvoidprintList(Nodehead){// return if list is emptyif(head==null)return;// initialize current node as headNodecurr=head;// loop through the circular linked listdo{// print the data of current nodeConsole.Write(curr.data+" ");// move to the next nodecurr=curr.next;}while(curr!=head);}publicstaticvoidMain(){// 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(x){this.data=x;this.next=null;}}functionprintList(head){// return if list is emptyif(head===null)return;// initialize current node as headletcurr=head;// loop through the circular linked listdo{// print the data of current nodeprocess.stdout.write(curr.data+' ');// move to the next nodecurr=curr.next;}while(curr!==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);