Open In App

Convert Singly Linked List into Circular Linked List using JavaScript

We are given a singly linked list and we need to convert it into a circular linked list, where the last node points back to the head of the list. Converting a singly linked list into a circular linked list involves making the last node's next pointer point back to the head, creating a circular structure. This transformation enables traversal from the last node to the first node in a loop.

Example:

Input: 1 -> 2 -> 3 -> 4 -> null
Output: 1 -> 2 -> 3 -> 4 -> 1 (Circular)

Below are the approaches to convert a singly linked list to a circular linked list in JavaScript:

Iterative approach

In the iterative approach, we will traverse the linked list to find the last node. Once we find the last node, we set its next pointer to the head of the linked list to make it circular linked list. We use a while loop to iterate through the linked list.

Example: Below code converts the singly linked list into circular linked list using Iterative approach.

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

class LinkedList {
    constructor() {
        this.head = null;
    }

    add(data) {
        const newNode = new Node(data);
        if (!this.head) {
            this.head = newNode;
        } else {
            let current = this.head;
            while (current.next) {
                current = current.next;
            }
            current.next = newNode;
        }
    }

    convertToCircular() {
        if (!this.head) return;

        let current = this.head;
        while (current.next) {
            current = current.next;
        }
        current.next = this.head;
    }

    printCircular() {
        if (!this.head) return;

        let current = this.head;
        let output = "";
        do {
            output += `${current.data} -> `;
            current = current.next;
        } while (current !== this.head);
        output += current.data;
        console.log(output);
    }
}

const list = new LinkedList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);

list.convertToCircular();

list.printCircular();

Output
1 -> 2 -> 3 -> 4 -> 1

Time Complexity: O(n)

Space Complexity: O(1)

Recursive approach

In this approach, we define a helper function that takes two arguments a node and the head of the linked list. This function recursively traverses the linked list until it reaches the last node. Once the last node is reached, it sets its next pointer to the head, making the list circular.

Example: Below code converts the singly linked list into circular linked list using Recursive approach.

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

class LinkedList {
    constructor() {
        this.head = null;
    }

    add(data) {
        const newNode = new Node(data);
        if (!this.head) {
            this.head = newNode;
        } else {
            let current = this.head;
            while (current.next) {
                current = current.next;
            }
            current.next = newNode;
        }
    }

    Circular(node = this.head) {
        if (!node) return null;

        if (!node.next) {
            node.next = this.head;
            return this.head;
        }

        return this.Circular(node.next);
    }

    printCircular() {
        if (!this.head) return;

        let current = this.head;
        let output = "";
        do {
            output += `${current.data} -> `;
            current = current.next;
        } while (current !== this.head);
        output += current.data;
        console.log(output);
    }
}

const list = new LinkedList();
list.add(2);
list.add(9);
list.add(4);
list.add(3);
list.add(6);

list.Circular();
list.printCircular();

Output
2 -> 9 -> 4 -> 3 -> 6 -> 2

Time Complexity: O(n)

Space Complexity: O(1)

Article Tags :