Bird
Raised Fist0
DSA C#programming~10 mins

Insert at End of Circular Linked List in DSA C# - 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
No↓
Start at head
↓
Traverse to last node (node.next == head)?
↓
last_node.next = new_node
↓
new_node.next = head
↓
Done
Create a new node, if list is empty point it to itself and set head. Otherwise, find last node, link it to new node, and new node back to head.
Execution Sample
DSA C#
Node newNode = new Node(40);
if (head == null) {
  newNode.next = newNode;
  head = newNode;
} else {
  Node temp = head;
  while (temp.next != head) temp = temp.next;
  temp.next = newNode;
  newNode.next = head;
}
Insert a node with value 40 at the end of a circular linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=4010 -> 20 -> 30 -> (back to 10)newNode created, no pointer changes yetā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head ↑ last node
2Check if list empty (head == null)10 -> 20 -> 30 -> (back to 10)head not null, proceed to traverseSame as step 1
3Traverse to last node (temp starts at head=10)10 -> 20 -> 30 -> (back to 10)temp moves: 10 -> 20ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head temp
4Continue traversal to last node10 -> 20 -> 30 -> (back to 10)temp moves: 20 -> 30 (last node)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head temp (last node)
5Set last node's next to newNode10 -> 20 -> 30 -> 40temp.next = newNodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ data:40│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next: āˆ…ā”‚ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head ↑ last node newNode
6Set newNode.next to head10 -> 20 -> 30 -> 40 -> (back to 10)newNode.next = headā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ data:40│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head new tail
7Done10 -> 20 -> 30 -> 40 -> (back to 10)List updated with new node at endā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ data:40│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head new tail
šŸ’” Traversal stops when temp.next == head, indicating last node found
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
headNode(10)Node(10)Node(10)Node(10)Node(10)Node(10)
tempnullNode(20)Node(30)Node(30)Node(30)Node(30)
newNodenullNode(40)Node(40)Node(40)Node(40)Node(40)
temp.nextNode(20)Node(30)Node(10)Node(40)Node(40)Node(40)
newNode.nextnullnullnullnullNode(10)Node(10)
Key Moments - 3 Insights
Why do we check temp.next == head instead of temp == null?
Because in a circular linked list, the last node points back to head, not null. So temp.next == head means we reached the last node. See execution_table step 4.
What happens if the list is empty when inserting?
We make the new node point to itself and set head to new node, creating a single-node circular list. See execution_table step 2 for the check and concept_flow.
Why do we set newNode.next = head after linking last node to newNode?
To maintain the circular structure, newNode must point back to head. Otherwise, the list would break. See execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, which node is temp pointing to?
ANode with data 30
BNode with data 20
CNode with data 10
DNode with data 40
šŸ’” Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 4 in execution_table.
At which step does the new node's next pointer get assigned to head?
AStep 5
BStep 6
CStep 3
DStep 2
šŸ’” Hint
Look at the 'Pointer Changes' column in execution_table for when newNode.next is set.
If the list was empty initially, what would newNode.next point to after insertion?
Anull
Bhead (newNode itself)
Cprevious last node
Dhead's next node
šŸ’” Hint
Refer to concept_flow and key_moments about empty list insertion.
Concept Snapshot
Insert at End of Circular Linked List:
- Create new node
- If list empty: newNode.next = newNode; head = newNode
- Else traverse to last node (node.next == head)
- last_node.next = newNode
- newNode.next = 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 and set head to this new node, forming a single-node circle. If not empty, start from head and traverse nodes until you find the last node, identified when its next points back to head. Then, link the last node's next to the new node, and set the new node's next to head to keep the circle intact. This process ensures the list remains circular with the new node at the end.

Practice

(1/5)
1. What happens when you insert a new node at the end of a circular linked list?
easy
A. The new node points back to the head, maintaining the circular structure.
B. The new node points to null, breaking the circular structure.
C. The new node becomes the head and points to null.
D. The new node is inserted at the beginning, not the end.

Solution

  1. Step 1: Understand circular linked list structure

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

    The new node is linked after the current last node, and its next pointer is set to the head, preserving the circle.
  3. Final Answer:

    The new node points back to the head, maintaining the circular structure. -> Option A
  4. Quick Check:

    New node next = head keeps list circular [OK]
Hint: New end node must link to head to keep circle [OK]
Common Mistakes:
  • Setting new node's next to null breaks the circle
  • Inserting new node at head instead of end
  • Forgetting to update last node's next pointer
2. Which of the following C# code snippets correctly inserts a new node at the end of a circular linked list?
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 = head.next; lastNode.next = head;

Solution

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

    To maintain circularity, newNode.next must point to head node.
  2. Step 2: Link last node to new node

    Update lastNode.next to newNode to insert at 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 = head; last node next = new node [OK]
Common Mistakes:
  • Setting new node next to null breaks circle
  • Linking new node to last node instead of head
  • Not updating last node's next pointer
3. Given the circular linked list: 10 -> 20 -> 30 -> back to 10, what is the list after inserting 40 at the end?
medium
A. 40 -> 10 -> 20 -> 30 -> back to 40
B. 10 -> 20 -> 30 -> back to 40
C. 10 -> 20 -> 30 -> 40 -> back to 10
D. 10 -> 20 -> 30 -> 40 -> null

Solution

  1. Step 1: Identify current circular list

    List is 10 -> 20 -> 30, last node 30 points back to 10.
  2. Step 2: Insert 40 at end

    New node 40 is linked after 30, and 40 points back to 10 to keep circle.
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> back to 10 -> Option C
  4. Quick Check:

    New node added at end, circle maintained [OK]
Hint: New node added after last, points to head [OK]
Common Mistakes:
  • Making new node head instead of last
  • Breaking circle by pointing new node to null
  • Forgetting to update last node's next pointer
4. What is wrong with this code snippet for inserting at the end of a circular linked list?
newNode.next = null;
lastNode.next = newNode;
newNode.next = head;
medium
A. The code correctly inserts the node without issues.
B. Setting newNode.next to null breaks the circular link temporarily.
C. newNode.next should point to lastNode, not head.
D. lastNode.next should not be updated before newNode.next.

Solution

  1. Step 1: Analyze newNode.next assignment

    Setting newNode.next = null breaks the circular link temporarily, which is unnecessary.
  2. Step 2: Check order of pointer updates

    Correct insertion requires newNode.next to point to head immediately to maintain circle.
  3. Final Answer:

    Setting newNode.next to null breaks the circular link temporarily. -> Option B
  4. Quick Check:

    newNode.next must never be null in circular list [OK]
Hint: Never set new node's next to null in circular list [OK]
Common Mistakes:
  • Assigning null to newNode.next
  • Updating lastNode.next before setting newNode.next
  • Confusing pointer directions
5. You have a circular linked list with only one node (value 5) pointing to itself. After inserting a new node with value 10 at the end, what is the correct structure?
hard
A. 5 -> 10 -> null
B. 5 -> back to 10 -> back to 5
C. 10 -> 5 -> back to 10
D. 5 -> 10 -> back to 5

Solution

  1. Step 1: Understand single-node circular list

    Initially, node 5 points to itself, forming a circle.
  2. Step 2: Insert new node 10 at end

    New node 10 is linked after 5, and 10 points back to 5 to maintain circularity.
  3. Final Answer:

    5 -> 10 -> back to 5 -> Option D
  4. Quick Check:

    Two nodes linked circularly, last points to head [OK]
Hint: New node next = head even if list has one node [OK]
Common Mistakes:
  • Pointing new node to null instead of head
  • Not updating original node's next pointer
  • Confusing order of nodes 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