Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Insert at Beginning of Circular Linked List in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular 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 after inserting 10 at the beginning?
Given a circular linked list with nodes 20 -> 30 -> 40 -> back to 20, what is the printed list after inserting 10 at the beginning?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

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

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

const cll = new CircularLinkedList();
cll.insertAtBeginning(40);
cll.insertAtBeginning(30);
cll.insertAtBeginning(20);
cll.insertAtBeginning(10);
console.log(cll.printList());
A10 -> 20 -> 30 -> 40 -> back to 10
B20 -> 30 -> 40 -> 10 -> back to 20
C40 -> 30 -> 20 -> 10 -> back to 40
D10 -> 40 -> 30 -> 20 -> back to 10
Attempts:
2 left
💡 Hint
Remember that inserting at the beginning updates the head and the last node's next pointer.
❓ Predict Output
intermediate
2:00remaining
What is the output after inserting into an empty circular linked list?
What will be printed after inserting 5 into an empty circular linked list?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

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

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

const cll = new CircularLinkedList();
cll.insertAtBeginning(5);
console.log(cll.printList());
A5 -> 5 -> back to 5
Bback to 5
C5 -> back to 5
D5 -> null
Attempts:
2 left
💡 Hint
In a circular linked list with one node, the node points to itself.
❓ Predict Output
advanced
2:00remaining
What is the output after multiple insertions at beginning?
Starting with an empty circular linked list, insert 1, then 2, then 3 at the beginning. What is the printed list?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

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

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

const cll = new CircularLinkedList();
cll.insertAtBeginning(1);
cll.insertAtBeginning(2);
cll.insertAtBeginning(3);
console.log(cll.printList());
A1 -> 3 -> 2 -> back to 1
B1 -> 2 -> 3 -> back to 1
C2 -> 3 -> 1 -> back to 2
D3 -> 2 -> 1 -> back to 3
Attempts:
2 left
💡 Hint
Each new insertion at beginning becomes the new head.
❓ Predict Output
advanced
2:00remaining
What error occurs if we forget to update last node's next pointer?
In the insertAtBeginning method, if we do not update the last node's next pointer to the new head, what will happen when printing the list?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

  insertAtBeginning(data) {
    const newNode = new Node(data);
    if (!this.head) {
      newNode.next = newNode;
      this.head = newNode;
    } else {
      // Missing update of last node's next pointer
      newNode.next = this.head;
      this.head = newNode;
    }
  }

  printList() {
    if (!this.head) return '';
    let result = '';
    let temp = this.head;
    let count = 0;
    do {
      result += temp.data + ' -> ';
      temp = temp.next;
      count++;
      if (count > 10) return 'Infinite loop detected';
    } while (temp !== this.head);
    result += 'back to ' + this.head.data;
    return result;
  }
}

const cll = new CircularLinkedList();
cll.insertAtBeginning(1);
cll.insertAtBeginning(2);
cll.insertAtBeginning(3);
console.log(cll.printList());
AInfinite loop detected
BTypeError: Cannot read property 'next' of null
C3 -> 2 -> 1 -> null
D3 -> 2 -> 1 -> back to 3
Attempts:
2 left
💡 Hint
The last node must point to the new head to keep the list circular.
🧠 Conceptual
expert
2:00remaining
Why is updating the last node's next pointer necessary when inserting at beginning?
In a circular linked list, when inserting a new node at the beginning, why must we update the last node's next pointer to the new head?
ATo prevent the list from becoming doubly linked
BTo maintain the circular structure so traversal loops back to the new head
CTo allow the new node to point to the previous head's next node
DTo increase the size of the list by one
Attempts:
2 left
💡 Hint
Think about what keeps the list circular.

Practice

(1/5)
1. What happens when you insert a new node at the beginning of a circular linked list?
easy
A. The new node becomes the head and points to the old head, maintaining the circular link.
B. The new node is added at the end, breaking the circular link.
C. The new node replaces the last node in the list.
D. The list becomes linear instead of circular.

Solution

  1. Step 1: Understand the circular linked list structure

    In a circular linked list, the last node points back to the head node, forming a circle.
  2. Step 2: Insert new node at beginning

    The new node is linked to the current head, and the last node's next pointer is updated to this new node, making it the new head.
  3. Final Answer:

    The new node becomes the head and points to the old head, maintaining the circular link. -> Option A
  4. Quick Check:

    New head points to old head, circle intact [OK]
Hint: New head points to old head; last node points to new head [OK]
Common Mistakes:
  • Forgetting to update last node's next pointer
  • Breaking the circular link by not linking last node to new head
  • Inserting at end instead of beginning
2. Which of the following is the correct syntax to create a new node and insert it at the beginning of a circular linked list in JavaScript?
easy
A. const newNode = { data: value, next: head }; last.next = newNode; head = newNode;
B. const newNode = { data: value, next: null }; last.next = head; head = newNode;
C. const newNode = { data: value, next: head }; head.next = newNode; head = newNode;
D. const newNode = { data: value, next: null }; newNode.next = head; head = newNode;

Solution

  1. Step 1: Create new node with next pointing to current head

    The new node's next should point to the current head to insert at beginning.
  2. Step 2: Update last node's next to new node and update head

    Since it's circular, last node's next must point to new node, then head is updated to new node.
  3. Final Answer:

    const newNode = { data: value, next: head }; last.next = newNode; head = newNode; -> Option A
  4. Quick Check:

    New node next = head, last.next = new node, head updated [OK]
Hint: New node next = head; last.next = new node; update head [OK]
Common Mistakes:
  • Not updating last.next to new node
  • Setting newNode.next to null instead of head
  • Updating head before last.next
3. What will be the output of the following code snippet after inserting 10 at the beginning of the circular linked list?
function Node(data) { this.data = data; this.next = null; }

let head = new Node(20);
let second = new Node(30);
head.next = second;
second.next = head; // circular link

// Insert 10 at beginning
const newNode = new Node(10);
newNode.next = head;
second.next = newNode;
head = newNode;

// Print list starting from head
let temp = head;
do {
  console.log(temp.data);
  temp = temp.next;
} while(temp !== head);
medium
A. 20 30 10
B. 10 20 30
C. 10 30 20
D. 30 20 10

Solution

  1. Step 1: Understand the initial circular list

    Initially, list is 20 -> 30 -> back to 20.
  2. Step 2: Insert 10 at beginning and update links

    New node 10 points to 20 (old head), 30 (last node) points to 10, head updated to 10.
  3. Step 3: Print order starting from new head

    Traversal prints 10, then 20, then 30, then stops as it loops back to head.
  4. Final Answer:

    10 20 30 -> Option B
  5. Quick Check:

    New head 10, then 20, then 30 [OK]
Hint: New head data prints first, then follow next pointers [OK]
Common Mistakes:
  • Not updating last node's next to new head
  • Printing nodes until null instead of circular condition
  • Confusing node order after insertion
4. Identify the error in the following code that inserts a node at the beginning of a circular linked list:
function insertAtBeginning(head, value) {
  const newNode = { data: value, next: head };
  let temp = head;
  while(temp.next !== null) {
    temp = temp.next;
  }
  temp.next = newNode;
  head = newNode;
  return head;
}
medium
A. The function should not return head.
B. The newNode's next should be null, not head.
C. The head should not be updated after insertion.
D. The while loop condition should check for temp.next !== head instead of null.

Solution

  1. Step 1: Analyze loop condition for circular list

    In a circular linked list, last node's next points to head, not null, so loop should check temp.next !== head.
  2. Step 2: Understand impact of wrong condition

    Checking for null causes infinite loop or error because next is never null in circular list.
  3. Final Answer:

    The while loop condition should check for temp.next !== head instead of null. -> Option D
  4. Quick Check:

    Loop until last node points to head [OK]
Hint: In circular list, last.next points to head, not null [OK]
Common Mistakes:
  • Using null check in circular list traversal
  • Not updating last node's next pointer correctly
  • Forgetting to update head after insertion
5. You have a circular linked list with nodes containing values 5, 15, and 25. You want to insert a new node with value 10 at the beginning. After insertion, what will be the correct order of nodes when traversed starting from the new head, and why is updating the last node's next pointer essential?
hard
A. 5 -> 15 -> 25 -> 10; last.next should point to old head, not new node.
B. 10 -> 5 -> 15 -> 25; updating last.next is optional since newNode.next points to head.
C. 10 -> 5 -> 15 -> 25; updating last.next maintains the circular link to new head.
D. 10 -> 15 -> 25 -> 5; last.next should point to newNode.next.

Solution

  1. Step 1: Insert new node at beginning and update pointers

    New node 10 points to old head 5; last node 25 must point to new node 10 to keep circular link.
  2. Step 2: Traverse list from new head

    Traversal order is 10 -> 5 -> 15 -> 25, then back to 10.
  3. Step 3: Importance of updating last.next

    If last.next is not updated to new head, circular link breaks and traversal fails.
  4. Final Answer:

    10 -> 5 -> 15 -> 25; updating last.next maintains the circular link to new head. -> Option C
  5. Quick Check:

    Last node points to new head to keep circle [OK]
Hint: Last node must point to new head to keep circle intact [OK]
Common Mistakes:
  • Skipping update of last.next pointer
  • Assuming newNode.next update is enough
  • Incorrect traversal order after insertion
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