Data Structure Circular Linked List Practice Questions with Solutions

Introductions

Circular linked lists are useful when the last node needs to connect back to the first node. In these practice questions, you will work with creating circular linked lists, traversing them safely, counting nodes, searching, inserting nodes, deleting nodes, and understanding how circular links behave. The examples use JavaScript and start with simple operations before moving toward more practical circular linked list problems. Data Structure Circular Linked List practice questions with solutions help to build concepts.

Question 1: Create a Circular Linked List

Question

Create a circular linked list containing:

10 → 20 → 30
↑         ↓
└─────────┘

Connect the last node back to the first node and print all values once.

Solution

A circular linked list is different from a normal singly linked list because the last node does not point to null.

Instead:

30.next → 10

Create the nodes:

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

let head = new Node(10);
let second = new Node(20);
let third = new Node(30);

Connect the nodes:

head.next = second;
second.next = third;
third.next = head;

The structure is now:

10 → 20 → 30
↑         ↓
└─────────┘

Because there is no null at the end, we should not use:

while (current !== null)

Instead, stop when we reach the head again.

let current = head;

do {
    console.log(current.data);
    current = current.next;
} while (current !== head);

Output

10
20
30

Answer

The circular linked list is successfully created:

10 → 20 → 30 → 10 → 20 → ...

For one complete traversal, we stop when we reach head again.


Question 2: Traverse a Circular Linked List

Question

Print every element exactly once from this circular linked list:

5 → 10 → 15 → 20 → 25
↑                   ↓
└───────────────────┘

Solution

In a circular linked list, the last node points back to the first node.

Therefore, current will never become null.

We can use a do...while loop and stop when current becomes head again.

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

let head = new Node(5);
head.next = new Node(10);
head.next.next = new Node(15);
head.next.next.next = new Node(20);
head.next.next.next.next = new Node(25);

head.next.next.next.next.next = head;

let current = head;

do {
    console.log(current.data);
    current = current.next;
} while (current !== head);

The traversal is:

5 → 10 → 15 → 20 → 25
                     ↓
                     5

When current becomes head, one complete round has finished.

Output

5
10
15
20
25

Answer

A circular linked list should be traversed carefully because there is no null at the end.


Question 3: Count Nodes in a Circular Linked List

Question

Count the number of nodes in:

10 → 20 → 30 → 40 → 50 → back to 10

Solution

We start from head and count each node.

The loop stops when we reach head again.

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

let head = new Node(10);

head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(50);

head.next.next.next.next.next = head;

let count = 0;
let current = head;

do {
    count++;
    current = current.next;
} while (current !== head);

console.log(count);

The counting works like this:

10 → count = 1
20 → count = 2
30 → count = 3
40 → count = 4
50 → count = 5

Then current comes back to head.

Output

5

Answer

The circular linked list contains 5 nodes.


Question 4: Search for an Element

Question

Search for 30 in:

10 → 20 → 30 → 40 → 50 → back to 10

Print "Found" if the value exists.

Solution

We cannot search until current === null because a circular linked list never reaches null.

Instead, stop when we return to head.

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

let head = new Node(10);

head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(50);

head.next.next.next.next.next = head;

let target = 30;
let current = head;
let found = false;

do {
    if (current.data === target) {
        found = true;
        break;
    }

    current = current.next;
} while (current !== head);

if (found) {
    console.log("Found");
} else {
    console.log("Not Found");
}

The search checks:

10 → Not Found
20 → Not Found
30 → Found

Output

Found

Answer

The value 30 exists in the circular linked list.


Question 5: Insert a Node at the Beginning

Question

Given:

20 → 30 → 40 → back to 20

Insert 10 at the beginning.

Solution

This operation requires changing two links.

Initially:

20 → 30 → 40
↑         ↓
└─────────┘

Create the new node:

let newNode = new Node(10);

We need to find the last node because its next currently points to head.

let last = head;

while (last.next !== head) {
    last = last.next;
}

Now last points to 40.

Connect the new node to the old head:

newNode.next = head;

Make the last node point to the new node:

last.next = newNode;

Finally, update the head:

head = newNode;

The structure becomes:

10 → 20 → 30 → 40
↑              ↓
└──────────────┘

Complete code:

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

let head = new Node(20);

head.next = new Node(30);
head.next.next = new Node(40);

head.next.next.next = head;

let newNode = new Node(10);

let last = head;

while (last.next !== head) {
    last = last.next;
}

newNode.next = head;
last.next = newNode;
head = newNode;

let current = head;

do {
    console.log(current.data);
    current = current.next;
} while (current !== head);

Output

10
20
30
40

Answer

10 is successfully inserted at the beginning of the circular linked list.


Question 6: Insert a Node at the End

Question

Given:

10 → 20 → 30 → back to 10

Insert 40 at the end.

Solution

Create the new node:

let newNode = new Node(40);

Find the last node:

let last = head;

while (last.next !== head) {
    last = last.next;
}

After the loop:

last → 30

Now connect the new node:

last.next = newNode;
newNode.next = head;

The list becomes:

10 → 20 → 30 → 40
↑              ↓
└──────────────┘

Complete code:

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

let head = new Node(10);

head.next = new Node(20);
head.next.next = new Node(30);

head.next.next.next = head;

let newNode = new Node(40);

let last = head;

while (last.next !== head) {
    last = last.next;
}

last.next = newNode;
newNode.next = head;

let current = head;

do {
    console.log(current.data);
    current = current.next;
} while (current !== head);

Output

10
20
30
40

Answer

40 is successfully inserted at the end.


Question 7: Insert a Node After a Given Value

Question

Given:

10 → 20 → 40 → 50 → back to 10

Insert 30 after 20.

Solution

We first search for the node containing 20.

let current = head;

do {
    if (current.data === 20) {
        break;
    }

    current = current.next;
} while (current !== head);

Now current points to 20.

Create the new node:

let newNode = new Node(30);

Connect the new node:

newNode.next = current.next;
current.next = newNode;

Before:

20 → 40

After:

20 → 30 → 40

Complete code:

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

let head = new Node(10);

head.next = new Node(20);
head.next.next = new Node(40);
head.next.next.next = new Node(50);

head.next.next.next.next = head;

let current = head;

do {
    if (current.data === 20) {
        break;
    }

    current = current.next;
} while (current !== head);

let newNode = new Node(30);

newNode.next = current.next;
current.next = newNode;

current = head;

do {
    console.log(current.data);
    current = current.next;
} while (current !== head);

Output

10
20
30
40
50

Answer

30 is successfully inserted after 20.


Question 8: Delete the First Node

Question

Delete the first node from:

10 → 20 → 30 → 40 → back to 10

Solution

The first node is head.

However, we also need to find the last node because its next must be changed to the new head.

Find the last node:

let last = head;

while (last.next !== head) {
    last = last.next;
}

Move the head:

head = head.next;

Now connect the last node to the new head:

last.next = head;

Before:

10 → 20 → 30 → 40
↑              ↓
└──────────────┘

After:

20 → 30 → 40
↑         ↓
└─────────┘

Complete code:

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

let head = new Node(10);

head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);

head.next.next.next.next = head;

let last = head;

while (last.next !== head) {
    last = last.next;
}

head = head.next;
last.next = head;

let current = head;

do {
    console.log(current.data);
    current = current.next;
} while (current !== head);

Output

20
30
40

Answer

The first node containing 10 has been deleted.


Question 9: Delete a Node by Value

Question

Delete 30 from:

10 → 20 → 30 → 40 → 50 → back to 10

Solution

To delete 30, we need to find the node before it.

That node is 20.

Before deletion:

20 → 30 → 40

We need to change the link to:

20 → 40

We can check current.next.data to find the node that needs to be deleted.

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

let head = new Node(10);

head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(50);

head.next.next.next.next.next = head;

let target = 30;
let current = head;

do {
    if (current.next.data === target) {
        current.next = current.next.next;
        break;
    }

    current = current.next;
} while (current !== head);

current = head;

do {
    console.log(current.data);
    current = current.next;
} while (current !== head);

The structure changes from:

10 → 20 → 30 → 40 → 50

to:

10 → 20 → 40 → 50

Output

10
20
40
50

Answer

The node containing 30 has been successfully deleted.


Question 10: Find the Maximum Value

Question

Find the largest value in this circular linked list:

15 → 8 → 42 → 23 → 10 → back to 15

Solution

Start by assuming the first node contains the maximum value:

let max = head.data;

Then visit every node.

For every node:

if (current.data > max) {
    max = current.data;
}

Because this is a circular list, stop when we return to head.

Complete code:

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

let head = new Node(15);

head.next = new Node(8);
head.next.next = new Node(42);
head.next.next.next = new Node(23);
head.next.next.next.next = new Node(10);

head.next.next.next.next.next = head;

let max = head.data;
let current = head.next;

while (current !== head) {
    if (current.data > max) {
        max = current.data;
    }

    current = current.next;
}

console.log(max);

Let’s compare the values:

15 → max = 15
8  → max = 15
42 → max = 42
23 → max = 42
10 → max = 42

We have now returned to head.

Output

42

Answer

The maximum value in the circular linked list is 42.

Key Takeaways

  • A circular linked list has nodes connected in a circular manner.
  • The last node points back to the first node.
  • Unlike a normal singly linked list, the last node does not point to null.
  • Traversal should stop when the current node reaches head again.
  • A do...while loop is useful for traversing a circular linked list.
  • Searching requires checking whether the current node has returned to head.
  • Inserting at the beginning requires updating the last node’s next reference.
  • Inserting at the end requires connecting the new node back to head.
  • Deleting the first node requires updating both head and the last node’s next.
  • Deleting another node requires changing the previous node’s next reference.
  • Circular linked lists are useful when data needs to be processed repeatedly in a cycle.
  • Traversing all nodes generally takes O(n) time.
  • Searching for a value generally takes O(n) time.

FAQs

1. What is a circular linked list?

A circular linked list is a linked list where the last node points back to the first node instead of pointing to null.

2. What is the main difference between a singly linked list and a circular linked list?

In a normal singly linked list, the last node points to null. In a circular linked list, the last node points back to the head.

3. How do you traverse a circular linked list?

Start from the head and keep moving to next until the current node becomes the head again.

4. Why can’t we use current !== null for circular linked list traversal?

Because the last node points back to the head instead of null. Therefore, current will keep moving around the circle forever.

5. What is the time complexity of searching a circular linked list?

Searching a circular linked list generally takes O(n) time because, in the worst case, every node may need to be checked.

6. How do you insert a node at the beginning of a circular linked list?

Create the new node, make it point to the current head, update the last node to point to the new node, and then make the new node the new head.

7. Where are circular linked lists useful?

Circular linked lists are useful when elements need to be processed repeatedly in a cycle, such as round-robin scheduling, turn-based systems, and repeated playlists.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top