Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Insert at End of Circular Linked List
Create new node
↓
Is list empty?
Yes→new_node.next = new_node
↓
head = new_node
↓
Start at head
↓
Traverse until current.next == head
↓
Set current.next = new_node
↓
Set new_node.next = head
↓
Done
Create a new node, check if list is empty, if yes point new node to itself. Otherwise, traverse to last node, link last node to new node, and new node back to head.
Execution Sample
DSA Typescript
class Node {
  data: number;
  next: Node | null;
  constructor(data: number) {
    this.data = data;
    this.next = null;
  }
}

function insertEnd(head: Node | null, data: number): Node {
  const newNode = new Node(data);
  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, handling empty list case.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=4010 -> 20 -> 30 (circular)newNode.next = nullā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ head ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
2Check if list empty10 -> 20 -> 30 (circular)No changeSame as step 1
3Traverse to last node (current moves from 10 to 20)10 -> 20 -> 30 (circular)current moves to node with data 20Same as step 1, current at 20
4Traverse to last node (current moves from 20 to 30)10 -> 20 -> 30 (circular)current moves to node with data 30Same as step 1, current at 30
5Set current.next = newNode10 -> 20 -> 30 -> 40 (circular)30.next → newNodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ data:40│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next: āˆ…ā”‚ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
6Set newNode.next = head10 -> 20 -> 30 -> 40 (circular)40.next → head (10)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ data:40│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ head ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
7Return head10 -> 20 -> 30 -> 40 (circular)No changeFinal circular list with 4 nodes
šŸ’” Traversal stops when current.next == head (node with data 30), then insertion completes.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
headNode(10)Node(10)Node(10)Node(10)Node(10)Node(10)
newNodenullNode(40)Node(40)Node(40)Node(40)Node(40)
currentNode(10)Node(20)Node(30)Node(30)Node(30)Node(30)
newNode.nextnullnullnullnullNode(10)Node(10)
current.nextNode(20)Node(30)Node(10)Node(40)Node(40)Node(40)
Key Moments - 3 Insights
Why do we check current.next == head instead of current == null?
Because in a circular linked list, the last node points back to head, not null. So traversal stops when current.next is head (see execution_table step 4).
What happens if the list is empty when inserting?
If head is null, newNode.next points to itself, making a single-node circular list (see execution_table step 2).
Why do we set newNode.next = head after linking current.next to newNode?
To maintain the circular link so the new last node points back to head, preserving the circular structure (see execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of current after step 4?
ANode with data 40
BNode with data 30
CNode with data 20
DNode with data 10
šŸ’” Hint
Check the 'current' variable in variable_tracker after step 4.
At which step does the new node start pointing back to head?
AStep 5
BStep 3
CStep 6
DStep 7
šŸ’” Hint
Look at pointer changes in execution_table step 6.
If the list was empty initially, what would newNode.next point to after insertion?
AnewNode itself
Bhead
Cnull
Dlast node
šŸ’” Hint
Refer to execution_table step 2 for empty list case.
Concept Snapshot
Insert at End of Circular Linked List:
- Create new node
- If list empty: newNode.next = newNode (points to itself)
- Else traverse to last node (where next points to head)
- Set last node's next to newNode
- Set newNode.next to head
- Maintains circular structure
Full Transcript
To insert at the end of a circular linked list, first create a new node. If the list is empty, point the new node's next to itself, making a single-node circular list. Otherwise, start at head and traverse nodes until reaching the last node, identified when current.next equals head. Then, link the last node's next to the new node, and set the new node's next back to head to keep the list circular. Finally, return the original head. This process ensures the circular linked list structure is maintained after insertion.

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 null, ending the list.
B. The new node points to itself, becoming the only node in the list.
C. The list remains empty because insertion is not possible.
D. The new node points to the head, but the list has multiple nodes.

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 the head of the list.
  3. Final Answer:

    The new node points to itself, becoming the only node in the list. -> Option B
  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 updating head pointer to new node
2. Which of the following TypeScript code snippets correctly inserts a new node at the end of a circular linked list?
easy
A. newNode.next = head; head.next = newNode; tail = newNode;
B. newNode.next = null; tail.next = newNode; tail = newNode;
C. newNode.next = head; tail.next = newNode; tail = newNode;
D. newNode.next = tail; head.next = newNode; tail = 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.
  2. Step 2: Update tail's next and tail pointer

    Tail's next is updated to newNode, and tail pointer moves to newNode to mark new end.
  3. Final Answer:

    newNode.next = head; tail.next = newNode; tail = newNode; -> Option C
  4. Quick Check:

    New node points to head, tail updated [OK]
Hint: New node's next always points to head in circular list [OK]
Common Mistakes:
  • Setting newNode.next to null breaks circularity
  • Updating head.next instead of tail.next
  • Not moving tail pointer to new node
3. Given the circular linked list: 10 -> 20 -> 30 -> (back to 10), what is the list after inserting 40 at the end?
medium
A. 10 -> 20 -> 30 -> 40 -> 10
B. 40 -> 10 -> 20 -> 30 -> 40
C. 10 -> 20 -> 30 -> 40 -> null
D. 10 -> 20 -> 30 -> 10 -> 40

Solution

  1. Step 1: Understand circular list structure

    Original list loops from 30 back to 10, so last node 30 points to head 10.
  2. Step 2: Insert 40 at end and update pointers

    New node 40 points to head 10, old tail 30 points to 40, maintaining circularity.
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> 10 -> Option A
  4. Quick Check:

    New node added before head, tail points to new node [OK]
Hint: New tail points to head, list stays circular [OK]
Common Mistakes:
  • Adding null at end breaks circularity
  • Inserting new node at head instead of tail
  • Incorrect pointer updates causing loops
4. Identify the error in this TypeScript code snippet for inserting at the end of a circular linked list:
function insertEnd(tail: Node | null, data: number): Node {
  const newNode = new Node(data);
  if (!tail) {
    newNode.next = null;
    return newNode;
  }
  newNode.next = tail.next;
  tail.next = newNode;
  return newNode;
}
medium
A. newNode.next should point to itself when list is empty
B. Returning newNode instead of tail breaks list reference
C. newNode.next should point to tail.next, not null when list is empty
D. tail.next should not be updated in insertion

Solution

  1. Step 1: Check empty list insertion

    When tail is null (empty list), newNode.next should point to itself to maintain circularity, not null.
  2. Step 2: Verify pointer updates for non-empty list

    For non-empty list, newNode.next = tail.next (head), tail.next = newNode is correct.
  3. Final Answer:

    newNode.next should point to itself when list is empty -> Option A
  4. Quick Check:

    Empty list node points to itself [OK]
Hint: Empty circular list node points to itself, not null [OK]
Common Mistakes:
  • Setting newNode.next to null breaks circularity
  • Returning newNode instead of updating tail reference
  • Not updating tail.next pointer
5. You have a circular linked list with nodes 5 -> 15 -> 25 -> (back to 5). You want to insert 35 at the end and then insert 2 at the beginning. What is the correct final circular linked list?
hard
A. 35 -> 2 -> 5 -> 15 -> 25 -> 35
B. 5 -> 15 -> 25 -> 35 -> 2 -> 5
C. 2 -> 5 -> 15 -> 25 -> 35 -> 5
D. 2 -> 5 -> 15 -> 25 -> 35 -> 2

Solution

  1. Step 1: Insert 35 at end

    After inserting 35, list is 5 -> 15 -> 25 -> 35 -> back to 5.
  2. Step 2: Insert 2 at beginning

    Insert at beginning means new node 2 points to current head 5, tail's next updated to 2, making 2 the new head.
  3. Step 3: Final circular list

    The list is 2 -> 5 -> 15 -> 25 -> 35 -> back to 2, maintaining circularity.
  4. Final Answer:

    2 -> 5 -> 15 -> 25 -> 35 -> 2 -> Option D
  5. Quick Check:

    New head inserted, tail points to new head [OK]
Hint: Tail's next always points to head in circular list [OK]
Common Mistakes:
  • Not updating tail.next after inserting at beginning
  • Inserting new node at end but not updating tail
  • Breaking circular link by pointing to wrong node
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