Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Create a Circular Singly Linked List in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular Singly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this code creating a circular singly linked list?
Consider the following JavaScript code that creates a circular singly linked list with three nodes. What will be printed when we traverse the list once starting from the head?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

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

  traverse() {
    let result = [];
    if (!this.head) return result;
    let current = this.head;
    do {
      result.push(current.value);
      current = current.next;
    } while (current !== this.head);
    return result;
  }
}

const list = new CircularSinglyLinkedList();
list.append(10);
list.append(20);
list.append(30);
console.log(list.traverse());
A[10, 20, 30, 10]
B[10, 20]
C[10, 20, 30]
D[]
Attempts:
2 left
💡 Hint
Remember that in a circular singly linked list, the last node points back to the head, so traversal stops when we reach the head again.
🧠 Conceptual
intermediate
1:30remaining
How many nodes are in the circular singly linked list after these operations?
Starting with an empty circular singly linked list, we perform these operations: 1. Append 5 2. Append 15 3. Append 25 4. Append 35 5. Remove the node with value 15 How many nodes remain in the list?
A5
B4
C2
D3
Attempts:
2 left
💡 Hint
Each append adds one node. Removing one node reduces the count by one.
🔧 Debug
advanced
2:00remaining
What error occurs when running this circular singly linked list code?
This code tries to append nodes to a circular singly linked list but causes an error. What error will it raise?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

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

const list = new CircularSinglyLinkedList();
list.append(1);
list.append(2);
list.append(3);
ATypeError: Cannot read property 'next' of null
BInfinite loop
CNo error, list created correctly
DReferenceError: current is not defined
Attempts:
2 left
💡 Hint
Check the while loop condition carefully. What happens when current.next never becomes null?
📝 Syntax
advanced
2:00remaining
Which option correctly implements the append method for a circular singly linked list?
Choose the correct append method code snippet that adds a new node to the end of a circular singly linked list.
A
append(value) {
  const newNode = new Node(value);
  if (!this.head) {
    this.head = newNode;
    newNode.next = this.head;
  } else {
    let current = this.head;
    while (current.next !== this.head) {
      current = current.next;
    }
    newNode.next = current.next;
    current.next = newNode;
  }
}
B
append(value) {
  const newNode = new Node(value);
  if (!this.head) {
    this.head = newNode;
    newNode.next = null;
  } else {
    let current = this.head;
    while (current.next !== null) {
      current = current.next;
    }
    current.next = newNode;
    newNode.next = null;
  }
}
C
append(value) {
  const newNode = new Node(value);
  if (!this.head) {
    this.head = newNode;
    newNode.next = this.head;
  } else {
    let current = this.head;
    while (current.next !== this.head) {
      current = current.next;
    }
    current.next = newNode;
    newNode.next = this.head;
  }
}
D
append(value) {
  const newNode = new Node(value);
  if (!this.head) {
    this.head = newNode;
  } else {
    let current = this.head;
    while (current.next !== this.head) {
      current = current.next;
    }
    current.next = newNode;
  }
  newNode.next = this.head;
}
Attempts:
2 left
💡 Hint
Remember to update the new node's next pointer before linking it to the current last node.
🚀 Application
expert
2:30remaining
What is the output after these operations on a circular singly linked list?
Given an empty circular singly linked list, perform these operations: 1. Append 100 2. Append 200 3. Append 300 4. Remove the head node 5. Traverse the list What is the output array after traversal?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

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

  removeHead() {
    if (!this.head) return;
    if (this.head.next === this.head) {
      this.head = null;
      return;
    }
    let current = this.head;
    while (current.next !== this.head) {
      current = current.next;
    }
    current.next = this.head.next;
    this.head = this.head.next;
  }

  traverse() {
    let result = [];
    if (!this.head) return result;
    let current = this.head;
    do {
      result.push(current.value);
      current = current.next;
    } while (current !== this.head);
    return result;
  }
}

const list = new CircularSinglyLinkedList();
list.append(100);
list.append(200);
list.append(300);
list.removeHead();
console.log(list.traverse());
A[200, 300]
B[100, 200, 300]
C[300, 200]
D[200]
Attempts:
2 left
💡 Hint
Removing the head means the second node becomes the new head and the last node points to it.

Practice

(1/5)
1. What is a key characteristic of a circular singly linked list compared to a regular singly linked list?
easy
A. The last node points back to the first node, forming a circle.
B. Each node has two pointers, one to the next and one to the previous node.
C. It stores data in a sorted order automatically.
D. It uses an array internally to store nodes.

Solution

  1. Step 1: Understand the structure of a circular singly linked list

    In a circular singly linked list, the last node's next pointer points back to the first node, creating a loop.
  2. Step 2: Compare with other linked lists

    Unlike doubly linked lists, circular singly linked lists have only one pointer per node and form a circle, not a linear chain.
  3. Final Answer:

    The last node points back to the first node, forming a circle. -> Option A
  4. Quick Check:

    Last node's next = first node [OK]
Hint: Remember: circular means last node links to first [OK]
Common Mistakes:
  • Confusing circular singly linked list with doubly linked list
  • Thinking nodes have two pointers
  • Assuming data is sorted automatically
2. Which of the following is the correct way to initialize an empty circular singly linked list in JavaScript?
easy
A. const head = { data: null, next: head };
B. const head = null; // no nodes yet
C. const head = { data: null, next: null };
D. const head = []; // empty array

Solution

  1. Step 1: Understand empty list initialization

    An empty circular singly linked list has no nodes, so the head pointer is null.
  2. Step 2: Check other options

    const head = { data: null, next: null }; creates a node with null data and next pointer, which is not empty. const head = { data: null, next: head }; references head before assignment, causing error. const head = []; // empty array uses an array, not a linked list.
  3. Final Answer:

    const head = null; // no nodes yet -> Option B
  4. Quick Check:

    Empty list head = null [OK]
Hint: Empty list means head is null, no nodes created [OK]
Common Mistakes:
  • Creating a node with null data instead of null head
  • Referencing head before declaration
  • Using arrays instead of linked list nodes
3. What will be the output of the following code snippet that creates a circular singly linked list with nodes 1, 2, 3 and prints the list once?
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

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

  printList() {
    if (!this.head) return '';
    let result = '';
    let temp = this.head;
    do {
      result += temp.data + ' -> ';
      temp = temp.next;
    } while (temp !== this.head);
    result += 'null';
    return result;
  }
}

const list = new CircularSinglyLinkedList();
list.append(1);
list.append(2);
list.append(3);
console.log(list.printList());
medium
A. null
B. 1 -> 2 -> 3 -> 1 -> 2 -> 3 -> null
C. 1 -> 2 -> 3
D. 1 -> 2 -> 3 -> null

Solution

  1. Step 1: Understand append method behavior

    Each new node is added at the end, and its next points to head, maintaining circularity.
  2. Step 2: Trace printList method

    printList starts at head and loops until it reaches head again, appending data with ' -> '. Finally adds 'null'. So output is '1 -> 2 -> 3 -> null'.
  3. Final Answer:

    1 -> 2 -> 3 -> null -> Option D
  4. Quick Check:

    Print once around circle = 1 -> 2 -> 3 -> null [OK]
Hint: Print loops until back to head once, then stops [OK]
Common Mistakes:
  • Printing infinite loop by not stopping at head
  • Missing 'null' at end
  • Printing nodes multiple times
4. Identify the bug in this code snippet that tries to append a node to a circular singly linked list:
append(data) {
  const newNode = new Node(data);
  if (!this.head) {
    this.head = newNode;
    newNode.next = null;
  } else {
    let temp = this.head;
    while (temp.next !== this.head) {
      temp = temp.next;
    }
    temp.next = newNode;
    newNode.next = this.head;
  }
}
medium
A. newNode.next should point to this.head when list is empty, not null.
B. The while loop condition should be temp.next === this.head.
C. The head should be set to newNode every time a node is appended.
D. The newNode should not be created inside the append method.

Solution

  1. Step 1: Check initialization for empty list

    When list is empty, newNode.next must point to head itself to maintain circularity, not null.
  2. Step 2: Verify loop and pointer updates

    The while loop and pointer updates are correct; only the initial next pointer is wrong.
  3. Final Answer:

    newNode.next should point to this.head when list is empty, not null. -> Option A
  4. Quick Check:

    Empty list node next = head, not null [OK]
Hint: Empty list node next must loop to itself [OK]
Common Mistakes:
  • Setting newNode.next to null breaks circular link
  • Wrong while loop condition
  • Resetting head incorrectly
5. You want to insert a new node with value 4 after the node with value 3 in a circular singly linked list. Given the list: 3 -> 1 -> 2 -> (back to 3), which code snippet correctly performs this insertion?
hard
A. let temp = head; while (temp.next !== head) { temp = temp.next; } const newNode = new Node(4); temp.next = newNode; newNode.next = head;
B. const newNode = new Node(4); newNode.next = head; head.next = newNode;
C. let temp = head; while (temp.data !== 3) { temp = temp.next; } const newNode = new Node(4); newNode.next = temp.next; temp.next = newNode;
D. head = new Node(4); head.next = oldHead;

Solution

  1. Step 1: Locate node with value 3

    Traverse from head until temp.data equals 3 to find the correct insertion point.
  2. Step 2: Insert new node after node 3

    Create newNode with data 4, set newNode.next to temp.next, then set temp.next to newNode to insert correctly.
  3. Final Answer:

    Traverse from head until finding node with value 3, then insert new node after it. -> Option C
  4. Quick Check:

    Insert after node 3 by adjusting pointers [OK]
Hint: Find node 3, insert new node by pointer swap [OK]
Common Mistakes:
  • Inserting at wrong position
  • Not updating pointers correctly
  • Replacing head incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard