Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Create a Circular Singly Linked List in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Create a Circular Singly Linked List
Create new node
↓
Is list empty?
No→Traverse to last node
↓
Set last.next = new node
↓
Set head = new node
↓
Set new node.next = head
↓
Done
Start by creating a new node. If the list is empty, set head to new node and point new node to itself. Otherwise, find the last node and link it to the new node, then link new node back to head.
Execution Sample
DSA Typescript
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) { this.value = value; }
}

class CircularLinkedList {
  head: Node | null = null;
  add(value: number) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      newNode.next = newNode;
    } else {
      let current = this.head;
      while (current.next !== this.head) {
        current = current.next!;
      }
      current.next = newNode;
      newNode.next = this.head;
    }
  }
}
This code adds nodes to a circular singly linked list, linking the last node back to the head.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with value 10NonenewNode created (10)null
2List empty? Yes, set head = newNode and newNode.next = newNode10head -> 10, 10.next -> 1010 -> 10 (points to itself)
3Create new node with value 2010newNode created (20)10 -> 10
4Traverse to last node (10)10current -> 1010 -> 10
5Set last.next = newNode (20)10, 2010.next -> 2010 -> 20
6Set newNode.next = head (10)10, 2020.next -> 1010 -> 20 -> 10 (circular)
7Create new node with value 3010, 20newNode created (30)10 -> 20 -> 10
8Traverse to last node (10 -> 20)10, 20current moves: 10 -> 2010 -> 20 -> 10
9Set last.next = newNode (30)10, 20, 3020.next -> 3010 -> 20 -> 30
10Set newNode.next = head (10)10, 20, 3030.next -> 1010 -> 20 -> 30 -> 10 (circular)
11End of add operations10, 20, 30No pointer changes10 -> 20 -> 30 -> 10 (circular)
💡 No more nodes to add, list is circular with last node pointing to head
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10Final
headnullnullNode(10)Node(10)Node(10)Node(10)Node(10)Node(10)Node(10)Node(10)Node(10)Node(10)
newNodenullNode(10)Node(10)Node(20)Node(20)Node(20)Node(20)Node(30)Node(30)Node(30)Node(30)null
currentnullnullnullnullNode(10)Node(10)Node(10)nullNode(20)Node(20)Node(20)null
Key Moments - 3 Insights
Why do we set newNode.next = newNode when the list is empty?
Because in a circular singly linked list with one node, the node must point to itself to maintain the circular property. See step 2 in execution_table where newNode.next points to itself.
Why do we traverse until current.next === head instead of current === null?
Because the list is circular, there is no null at the end. The last node points back to head, so we stop traversal when current.next is head. See steps 4 and 8 where traversal stops at last node.
How does the list maintain circularity after adding new nodes?
After adding a new node, we set the last node's next to the new node and the new node's next back to head, preserving the circle. See steps 5, 6, 9, and 10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what does newNode.next point to?
AIt points to head which is null
BIt points to null
CIt points to itself (newNode)
DIt points to the next new node
💡 Hint
Check the Pointer Changes and Visual State columns at step 2 in execution_table
At which step does the list first become circular?
AStep 1
BStep 2
CStep 5
DStep 10
💡 Hint
Look for when newNode.next points to itself or head in execution_table
If we add a node with value 40 after step 10, what will be the new last node's next pointer?
AIt will point to the new node (40)
BIt will point to null
CIt will point to head (10)
DIt will point to the previous last node (30)
💡 Hint
Refer to steps 6 and 10 where last.next is set to newNode and newNode.next is set to head
Concept Snapshot
Create a Circular Singly Linked List:
- Create new node
- If list empty: head = new node, newNode.next = newNode (points to itself)
- Else: traverse to last node (where next points to head)
- Set last.next = newNode
- Set newNode.next = head
- List is circular: last node points back to head
Full Transcript
To create a circular singly linked list, start by creating a new node. If the list is empty, set the head to this new node and make the new node point to itself to form a circle. If the list is not empty, find the last node by traversing until the node whose next points to head. Then, link this last node's next to the new node, and link the new node's next back to head. This keeps the list circular. Each addition updates pointers carefully to maintain the circle. The traversal stops when current.next equals head because the list has no null end. This method ensures the list always loops back to the start.

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 uses an array internally to store nodes.
D. The list always has a fixed size.

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 list types

    Regular singly linked lists end with a null pointer, doubly linked lists have two pointers per node, and arrays are not linked lists.
  3. Final Answer:

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

    Circular list last node points to head = A [OK]
Hint: Remember: circular means last node links back to first [OK]
Common Mistakes:
  • Thinking circular means doubly linked
  • Assuming last node points to null
  • Confusing arrays with linked lists
2. Which of the following TypeScript code snippets correctly initializes an empty circular singly linked list?
easy
A. let head: Node = null; head.next = head;
B. let head: Node | null = null;
C. let head: Node | null = new Node(null); head.next = null;
D. let head: Node = new Node(null); head.next = head;

Solution

  1. Step 1: Understand initialization of circular singly linked list

    For an empty circular singly linked list with one node, the node's next pointer should point to itself.
  2. Step 2: Analyze each option

    let head: Node = new Node(null); head.next = head; creates a node and sets its next to itself, correctly forming a circular list with one node. let head: Node | null = null; sets head to null (empty list, but not circular). let head: Node | null = new Node(null); head.next = null; sets next to null (not circular). let head: Node = null; head.next = head; tries to assign next on null (error).
  3. Final Answer:

    let head: Node = new Node(null); head.next = head; -> Option D
  4. Quick Check:

    One node points to itself = A [OK]
Hint: One node circular list points next to itself [OK]
Common Mistakes:
  • Setting next to null instead of self
  • Assigning next on null variable
  • Not creating a node before linking
3. Given the following TypeScript code for a circular singly linked list, what will be the output after inserting nodes with values 1, 2, and 3 in order?
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class CircularList {
  head: Node | null = null;

  insert(value: number) {
    const newNode = new Node(value);
    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;
    }
  }

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

const list = new CircularList();
list.insert(1);
list.insert(2);
list.insert(3);
console.log(list.print());
medium
A. 1 -> 2 -> 3 -> 1 -> null
B. 1 -> 2 -> 3 -> 1
C. 1 -> 2 -> 3 -> null
D. 1 -> 2 -> 3

Solution

  1. Step 1: Trace insertions and list structure

    Insert 1: head points to node 1, node 1 next points to itself.
    Insert 2: find last node (1), set last node's next to new node 2, new node 2 next points to head (1).
    Insert 3: find last node (2), set last node's next to new node 3, new node 3 next points to head (1).
  2. Step 2: Trace print method output

    Print starts at head (1), prints 1 -> 2 -> 3 -> then stops when it reaches head again, appends 'null'.
  3. Final Answer:

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

    Print stops at head, adds null = B [OK]
Hint: Print stops when node loops back to head [OK]
Common Mistakes:
  • Printing extra head node again
  • Missing 'null' at end
  • Infinite loop without stopping condition
4. Identify the bug in this TypeScript method for inserting a node into a circular singly linked list:
insert(value: number) {
  const newNode = new Node(value);
  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. The method does not create a new node correctly.
B. newNode.next should point to head, not null, when list is empty.
C. The while loop condition should be temp.next === null.
D. The head should be set to null after insertion.

Solution

  1. Step 1: Analyze insertion when list is empty

    When the list is empty, newNode.next is set to null, breaking circular property. It should point to head to form a circle.
  2. Step 2: Check loop condition and other logic

    The while loop correctly finds the last node by checking temp.next !== head. Other parts are correct.
  3. Final Answer:

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

    Empty list node next must point to itself = C [OK]
Hint: Empty list node next must point to itself, not null [OK]
Common Mistakes:
  • Setting next to null breaks circular link
  • Wrong loop condition causes infinite loop
  • Resetting head incorrectly
5. You want to implement a method delete(value: number) to remove a node with a given value from a circular singly linked list. Which approach correctly handles deleting the head node while maintaining the circular structure?
hard
A. Find the last node, set its next to head.next, then update head to head.next.
B. Set head to null directly without updating any other node.
C. Set head.next to null and keep head unchanged.
D. Remove the node by setting head to head.next without updating last node's next.

Solution

  1. Step 1: Understand deleting head in circular list

    To delete the head, we must update the last node's next pointer to the new head (head.next) to maintain the circle.
  2. Step 2: Analyze each option

    Find the last node, set its next to head.next, then update head to head.next. correctly finds last node, updates its next to head.next, then moves head to head.next.
    Set head to null directly without updating any other node. breaks the list by setting head to null without fixing last node.
    Set head.next to null and keep head unchanged. breaks the circle by setting head.next to null.
    Remove the node by setting head to head.next without updating last node's next. moves head but leaves last node pointing to old head, breaking the circle.
  3. Final Answer:

    Find the last node, set its next to head.next, then update head to head.next. -> Option A
  4. Quick Check:

    Last node next updated to new head = D [OK]
Hint: Update last node's next when deleting head [OK]
Common Mistakes:
  • Not updating last node's next pointer
  • Setting head to null without cleanup
  • Breaking circular link by null assignments
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