Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Insert at End of Circular Linked List in DSA Javascript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Insert at End of Circular Linked List
Create new node
↓
Is list empty?
↓
Set new node next to self
↓
Set head to new node
↓
Traverse until current.next == head
↓
Set current.next to new node
↓
Set new node.next to head
↓
Done
The flow starts by creating a new node. If the list is empty, the new node points to itself and becomes head. Otherwise, traverse to the last node and insert the new node after it, linking back to head.
Execution Sample
DSA Javascript
function insertEnd(head, data) {
  const newNode = { data, next: null };
  if (!head) {
    newNode.next = newNode;
    return newNode;
  }
  let current = head;
  while (current.next !== head) current = current.next;
  current.next = newNode;
  newNode.next = head;
  return head;
}
This code inserts a new node with given data at the end of a circular linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=10∅newNode: data=10, next=nullNo nodes yet
2Check if list is empty (head=null)∅Condition trueList empty
3Set newNode.next = newNode (self-loop)newNode(10)newNode.next → newNode┌────────┐ │ data:10│ │ next:─┐│ └───────┘│ └─→ (points to self)
4Return newNode as headnewNode(10)head → newNode┌────────┐ │ data:10│ │ next:─┐│ └───────┘│ └─→ (points to self) head
5Insert 20 at end: Create new nodenewNode(10)newNode2: data=20, next=nullExisting: 10 → points to self New: 20 → null
6Start traversal at head (10)newNode(10)current → head (10)┌────────┐ │ data:10│ │ next:─┐│ └───────┘│ └─→ (points to self) head/current
7Check current.next == head? (10.next == 10)newNode(10)Condition true, stop traversalAt last node (10)
8Set current.next = newNode2 (20)10 → 2010.next → 20┌────────┐ ┌────────┐ │ data:10│──→ │ data:20│ │ next:─┐│ │ next:null│ └───────┘│ └────────┘ └─────────────→
9Set newNode2.next = head (10)10 → 20 → 10 (circular)20.next → 10┌────────┐ ┌────────┐ │ data:10│──→ │ data:20│──→ │ next:─┐│ │ next:─┐│ └───────┘│ └───────┘│ └─────────────┘ head back to head
10Return head (10)10 → 20 → 10 (circular)head unchanged┌────────┐ ┌────────┐ │ data:10│──→ │ data:20│──→ │ next:─┐│ │ next:─┐│ └───────┘│ └───────┘│ └─────────────┘ head back to head
11Insert 30 at end: Create new node10 → 20 → 10 (circular)newNode3: data=30, next=nullExisting: 10 → 20 → 10 New: 30 → null
12Start traversal at head (10)10 → 20 → 10current → 10┌────────┐ ┌────────┐ │ data:10│──→ │ data:20│──→ │ next:─┐│ │ next:─┐│ └───────┘│ └───────┘│ └─────────────┘ head/current
13Check current.next == head? (10.next == 20)10 → 20 → 10Condition false, move current to 20current moves to 20
14Check current.next == head? (20.next == 10)10 → 20 → 10Condition true, stop traversalAt last node (20)
15Set current.next = newNode3 (30)10 → 20 → 3020.next → 30┌────────┐ ┌────────┐ ┌────────┐ │ data:10│──→ │ data:20│──→ │ data:30│ │ next:─┐│ │ next:─┐│ │ next:null│ └───────┘│ └───────┘│ └────────┘ └─────────────┘
16Set newNode3.next = head (10)10 → 20 → 30 → 10 (circular)30.next → 10┌────────┐ ┌────────┐ ┌────────┐ │ data:10│──→ │ data:20│──→ │ data:30│──→ │ next:─┐│ │ next:─┐│ │ next:─┐│ └───────┘│ └───────┘│ └───────┘│ └─────────────┘ head back to head
17Return head (10)10 → 20 → 30 → 10 (circular)head unchanged┌────────┐ ┌────────┐ ┌────────┐ │ data:10│──→ │ data:20│──→ │ data:30│──→ │ next:─┐│ │ next:─┐│ │ next:─┐│ └───────┘│ └───────┘│ └───────┘│ └─────────────┘ head back to head
18Stop: No more insertions10 → 20 → 30 → 10 (circular)End of operationsFinal circular list with 3 nodes
💡 No more insertions; list is circular with last node pointing to head
Variable Tracker
VariableStartAfter Step 4After Step 10After Step 17Final
headnullnode(10)node(10)node(10)node(10)
newNodeundefinednode(10)node(20)node(30)node(30)
currentundefinedundefinednode(10)node(20)node(20)
List Size01233
Key Moments - 3 Insights
Why do we set newNode.next = newNode when the list is empty?
Because in a circular linked list with one node, the node must point to itself to maintain the circular property. See execution_table step 3 where newNode.next points to itself.
Why do we stop traversal when current.next == head, not when current == null?
In circular lists, there is no null at the end. The last node points back to head. So we stop when current.next == head to find the last node. See execution_table steps 7 and 14.
What happens to the head pointer after insertion at end?
The head pointer remains unchanged because insertion is at the end. The new node links back to head. See execution_table steps 10 and 17 where head stays the same.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what is the condition result for current.next == head?
AFalse, traversal continues
BTrue, traversal stops
CError, current is null
DUndefined
💡 Hint
Check the 'Condition true, stop traversal' note in step 7 of execution_table
At which step does the list first become circular with one node pointing to itself?
AStep 2
BStep 5
CStep 3
DStep 10
💡 Hint
Look at pointer changes in step 3 where newNode.next points to itself
If we insert a new node with data=40 after step 17, what will be the new last node's next pointer?
APoints to head node with data=10
BPoints to null
CPoints to new node with data=40
DPoints to node with data=30
💡 Hint
In circular linked list, last node's next always points to head, see steps 9 and 16
Concept Snapshot
Insert at End of Circular Linked List:
- Create new node
- If list empty: newNode.next = newNode (self-loop), head = newNode
- Else traverse to last node (current.next == head)
- Set last node's next to newNode
- Set newNode.next to head
- Head remains unchanged
- List remains circular
Full Transcript
This visual trace shows how to insert a new node at the end of a circular linked list. First, a new node is created. If the list is empty, the new node points to itself and becomes the head. Otherwise, we start at head and traverse nodes until we find the last node, identified when current.next points back to head. Then, we link the last node's next to the new node, and the new node's next back to head, maintaining the circular structure. The head pointer does not change during insertion at the end. The trace includes pointer diagrams showing the circular links at each step, and variable tracking for head, current, and list size. Key moments clarify why the new node points to itself when alone, why traversal stops at current.next == head, and why head remains unchanged. The quiz questions test understanding of these steps and pointer changes.

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