Bird
Raised Fist0
DSA Javascriptprogramming

Insert at End of Circular Linked List in DSA Javascript

Choose your learning style10 modes available
Mental Model
A circular linked list connects the last node back to the first, forming a loop. Inserting at the end means adding a new node just before the first node and updating the last node's pointer.
Analogy: Imagine people sitting in a circle holding hands. Adding a new person at the end means placing them just before the first person and making sure the last person now holds the new person's hand.
head -> 1 -> 2 -> 3 -> (back to head)
          ↑                   ↓
          ←-------------------
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 (circular), insert value 4 at end
Goal: Add a new node with value 4 at the end, keeping the list circular
Step 1: Create new node with value 4
head -> 1 -> 2 -> 3 -> (back to head)
newNode(4) isolated
Why: We need a new node to insert
Step 2: Find last node (node with value 3) by traversing from head
head -> 1 -> 2 -> [curr->3] -> (back to head)
Why: We must find the last node to update its pointer
Step 3: Set last node's next pointer to new node (3 -> 4)
head -> 1 -> 2 -> 3 -> 4
                      ↓
                      ?
Why: Link last node to new node to insert it at end
Step 4: Set new node's next pointer to head (4 -> 1)
head -> 1 -> 2 -> 3 -> 4 -> (back to head)
          ↑                   ↓
          ←-------------------
Why: Maintain circular link by pointing new node back to head
Result:
head -> 1 -> 2 -> 3 -> 4 -> (back to head)
Annotated Code
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  insertAtEnd(value) {
    const newNode = new Node(value); // create new node
    if (!this.head) {
      newNode.next = newNode; // points to itself
      this.head = newNode; // list was empty
      return;
    }

    let curr = this.head;
    while (curr.next !== this.head) {
      curr = curr.next; // find last node
    }

    curr.next = newNode; // last node points to new node
    newNode.next = this.head; // new node points to head
  }

  printList() {
    if (!this.head) {
      console.log('List is empty');
      return;
    }
    let curr = this.head;
    let result = '';
    do {
      result += curr.value + ' -> ';
      curr = curr.next;
    } while (curr !== this.head);
    result += '(back to head)';
    console.log(result);
  }
}

// Driver code
const cll = new CircularLinkedList();
cll.insertAtEnd(1);
cll.insertAtEnd(2);
cll.insertAtEnd(3);
cll.insertAtEnd(4);
cll.printList();
const newNode = new Node(value); // create new node
create the new node to insert
if (!this.head) { newNode.next = newNode; // points to itself this.head = newNode; // list was empty return; }
handle empty list by pointing new node to itself and setting head
while (curr.next !== this.head) { curr = curr.next; // find last node }
traverse to find the last node which points back to head
curr.next = newNode; // last node points to new node
link last node to new node
newNode.next = this.head; // new node points to head
link new node back to head to maintain circularity
OutputSuccess
1 -> 2 -> 3 -> 4 -> (back to head)
Complexity Analysis
Time: O(n) because we traverse the list to find the last node once
Space: O(1) because we only create one new node and use constant extra space
vs Alternative: Compared to inserting at the beginning which is O(1), inserting at end requires traversal making it O(n)
Edge Cases
empty list
new node points to itself and becomes head
DSA Javascript
if (!this.head) {
  newNode.next = newNode; // points to itself
  this.head = newNode; // list was empty
  return;
}
single node list
last node is head itself, new node inserted after it and points back to head
DSA Javascript
while (curr.next !== this.head) {
  curr = curr.next; // find last node
}
When to Use This Pattern
When you see a problem asking to add elements to a circular linked list, use the insert at end pattern by finding the last node and updating pointers to keep the circle intact.
Common Mistakes
Mistake: Not updating the new node's next pointer to head, breaking the circular link
Fix: Always set newNode.next = this.head after linking last node to new node
Mistake: Not handling empty list case, causing errors when head is null
Fix: Check if head is null and initialize new node to point to itself and set head
Summary
Adds a new node at the end of a circular linked list while keeping the list circular.
Use when you need to append elements to the end of a circular linked list.
The key is to find the last node and update its next pointer to the new node, then point the new node back to head.

Practice

(1/5)
1. What happens when you insert a new node at the end of an empty circular linked list?
easy
A. The new node points to itself, becoming the only node in the list.
B. The new node points to null, ending the list.
C. The list remains empty because no insertion is possible.
D. The new node points to the head, but the list is not circular.

Solution

  1. Step 1: Understand empty circular linked list

    An empty circular linked list has no nodes, so inserting the first node means it must point to itself to maintain circularity.
  2. Step 2: Insert new node and set its next pointer

    The new node's next pointer is set to itself, making it the only node and forming a circular link.
  3. Final Answer:

    The new node points to itself, becoming the only node in the list. -> Option A
  4. Quick Check:

    Empty list insertion = node points to itself [OK]
Hint: First node in circular list points to itself [OK]
Common Mistakes:
  • Setting next pointer to null instead of itself
  • Assuming list remains empty after insertion
  • Not maintaining circular link for single node
2. Which of the following is the correct syntax to insert a new node at the end of a circular linked list in JavaScript?
easy
A. newNode.next = head; lastNode.next = newNode;
B. newNode.next = null; lastNode.next = newNode;
C. newNode.next = lastNode; head.next = newNode;
D. newNode.next = newNode; head = newNode;

Solution

  1. Step 1: Set new node's next to head

    In a circular linked list, the last node points to the head, so newNode.next must point to head to maintain circularity.
  2. Step 2: Update last node's next to new node

    The current last node's next pointer is updated to newNode, inserting it at the end.
  3. Final Answer:

    newNode.next = head; lastNode.next = newNode; -> Option A
  4. Quick Check:

    New node points to head, last node points to new node [OK]
Hint: New node next points to head, last node next points to new node [OK]
Common Mistakes:
  • Setting newNode.next to null breaks circularity
  • Swapping pointers incorrectly
  • Not updating last node's next pointer
3. What will be the printed circular linked list after inserting a node with value 4 at the end of the list: 1 -> 2 -> 3 -> (back to 1)?
medium
A. 4 -> 1 -> 2 -> 3 -> (back to 4)
B. 1 -> 2 -> 3 -> 4 -> (back to 1)
C. 1 -> 2 -> 3 -> (back to 4)
D. 1 -> 2 -> 3 -> 4 -> null

Solution

  1. Step 1: Identify current list structure

    The list currently has nodes 1, 2, 3 linked circularly, so last node 3 points back to 1.
  2. Step 2: Insert node with value 4 at end

    New node 4's next points to head (1), and node 3's next points to node 4, extending the circle.
  3. Final Answer:

    1 -> 2 -> 3 -> 4 -> (back to 1) -> Option B
  4. Quick Check:

    New node added at end, circle maintained [OK]
Hint: New node added after last, points to head [OK]
Common Mistakes:
  • Placing new node at start instead of end
  • Breaking circular link by pointing to null
  • Incorrectly updating pointers causing broken list
4. Given the code snippet below, what is the error when inserting at the end of a circular linked list?
function insertEnd(head, data) {
  let newNode = { value: data, next: null };
  if (!head) {
    head = newNode;
    newNode.next = head;
    return head;
  }
  let temp = head;
  while (temp.next !== head) {
    temp = temp.next;
  }
  temp.next = newNode;
  newNode.next = head;
  return head;
}
medium
A. Head is not updated when list is empty.
B. The while loop condition is incorrect and causes infinite loop.
C. New node is not created properly.
D. New node's next is not set to head, breaking circularity.

Solution

  1. Step 1: Check new node's next pointer

    New node's next is initialized to null and never updated to point to head, breaking the circular link.
  2. Step 2: Verify circularity after insertion

    Since temp.next points to newNode but newNode.next is null, the list is no longer circular.
  3. Final Answer:

    New node's next is not set to head, breaking circularity. -> Option D
  4. Quick Check:

    New node next must point to head [OK]
Hint: Always set new node's next to head in circular list [OK]
Common Mistakes:
  • Forgetting to link new node back to head
  • Incorrect loop condition causing infinite loop
  • Not handling empty list properly
5. You have a circular linked list with nodes 10 -> 20 -> 30 -> (back to 10). You want to insert a new node with value 40 at the end, but also keep the list sorted in ascending order. Which approach correctly inserts the node and maintains circularity and order?
hard
A. Insert 40 at the end as usual, then rotate the list to start at 40.
B. Insert 40 at the head and update last node's next to new node.
C. Traverse to find correct position for 40, insert it there, update pointers to keep circularity.
D. Insert 40 at the end and break circularity temporarily.

Solution

  1. Step 1: Find correct sorted position for new node

    Traverse the list to find where 40 fits between nodes to keep ascending order.
  2. Step 2: Insert new node at found position and update pointers

    Insert new node between nodes, update previous node's next to new node, and new node's next to next node, maintaining circularity.
  3. Final Answer:

    Traverse to find correct position for 40, insert it there, update pointers to keep circularity. -> Option C
  4. Quick Check:

    Sorted insert requires correct position and pointer updates [OK]
Hint: Insert in sorted order by finding correct spot, update pointers [OK]
Common Mistakes:
  • Inserting at end without sorting
  • Breaking circularity during insertion
  • Not updating last node's next pointer correctly
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