Open In App

Convert singly linked list into circular linked list

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

Given a singly linked list, the task is to convert it into a circular linked list.

Examples:

Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL
Output:

Traversal-of-Circular-Linked-List


Explanation: Singly linked list is converted to circular by pointing last node to head.


Input: head: 2 -> 4 -> 6 -> 8 -> 10 -> 12 -> NULL
Output:

Traversal-of-Circular-Linked-List-2


Explanation: Singly linked list is converted to circular by pointing last node to head.

[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>
using namespace std;

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

// Function that convert singly linked list
// into circular linked list.
void circular(Node *curr, Node *head) {

    // if last node, then point next ptr
    // to head Node
    if (curr->next == nullptr) {
        curr->next = head;
        return;
    }

    // otherwise traverse to the
    // next node
    circular(curr->next, head);
}

void printList(Node *head) {
    Node *curr = head;

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

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

int main() {

    // create a hard coded list
    // 10->12->14->16
    Node *head = new Node(10);
    head->next = new Node(12);
    head->next->next = new Node(14);
    head->next->next->next = new Node(16);

    circular(head, head);

    printList(head);

    return 0;
}
C Java Python C# JavaScript

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>
using namespace std;

class Node {
  public:
    int data;
    Node *next;
    Node(int x) {
        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;

    return head;
}

void printList(Node *head) {
    Node *curr = head;

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

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

int main() {

    // create a hard coded list
    // 10->12->14->16
    Node *head = new Node(10);
    head->next = new Node(12);
    head->next->next = new Node(14);
    head->next->next->next = new Node(16);

    head = circular(head);

    printList(head);

    return 0;
}
C Java Python C# JavaScript

Output
10 12 14 16 

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



Next Article

Similar Reads

three90RightbarBannerImg