Open In App

Traversal of Circular Linked List

Last Updated : 12 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a circular linked list, the task is to print all the elements of this circular linked list.

Example:

Input:

Traversal-of-Circular-Linked-List


Output: 1 2 3 4 5 6


Input:

Traversal-of-Circular-Linked-List-2


Output: 2 4 6 8 10 12

[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.

Below is the implementation of above approach:

C++
// C++ program to traverse a circular
// linked list.
#include <iostream>
using namespace std;

class Node {
  public:
    int data;
    Node *next;
    Node(int x) {
        data = x;
        next = nullptr;
    }
};

void printList(Node *curr, Node *head) {

    // return if list is empty
    if (head == nullptr)
        return;

    cout << curr->data << " ";

    if (curr->next == head)
        return;

    printList(curr->next, head);
}

int main() {
  
      // Create a hard-coded linked list
    // 11 -> 2 -> 56 -> 12
    Node *head = new Node(11);
    head->next = new Node(2);
    head->next->next = new Node(56);
    head->next->next->next = new Node(12);

    head->next->next->next->next = head;

    printList(head, head);

    return 0;
}
C Java Python C# JavaScript

Output
11 2 56 12 

Time Complexity: O(n), where n is the number of nodes in circular Linked List.
Auxiliary Space: O(n) 

[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.

Below is the implementation of above approach:

C++
// C++ program to traverse a circular
// linked list.
#include <iostream>
using namespace std;

class Node {
  public:
    int data;
    Node *next;
    Node(int x) {
        data = x;
        next = nullptr;
    }
};

void printList(Node *head) {

    // return if list is empty
    if (head == nullptr)
        return;

    Node *curr = head;
    do {
        cout << curr->data << " ";
        curr = curr->next;

    } while (curr != head);
    cout << endl;
}

int main() {
      
      // Create a hard-coded linked list
    // 11 -> 2 -> 56 -> 12
    Node *head = new Node(11);
    head->next = new Node(2);
    head->next->next = new Node(56);
    head->next->next->next = new Node(12);

    head->next->next->next->next = head;

    printList(head);

    return 0;
}
C Java Python C# JavaScript

Output
11 2 56 12 

Time Complexity: O(n), where n is the number of nodes in circular Linked List.
Auxiliary Space: O(1)

Related articles:



Next Article

Similar Reads

three90RightbarBannerImg