Bird
Raised Fist0
DSA C#programming~10 mins

Create a Circular Singly Linked List in DSA C# - Execution Trace

Choose your learning style10 modes available
Concept Flow - Create a Circular Singly Linked List
Create new node with value
↓
Is list empty?
Yes→Set head = new node
↓
Set new node.next = head
↓
Traverse to last node
↓
Set last node.next = new node
↓
Set new node.next = head
↓
Done
Create a new node, if list empty set head and point to itself. Otherwise, find last node, link it to new node, and new node points back to head.
Execution Sample
DSA C#
Node newNode = new Node(10);
if (head == null) {
  head = newNode;
  newNode.next = head;
} else {
  Node temp = head;
  while (temp.next != head) temp = temp.next;
  temp.next = newNode;
  newNode.next = head;
}
This code adds a new node with value 10 to a circular singly linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with value 10EmptynewNode creatednull
2Check if head is nullEmptyhead is nullnull
3Set head = newNode1 nodehead -> newNode10 -> ?
4Set newNode.next = head1 nodenewNode.next -> head10 -> 10 (circular)
5Add node complete1 nodehead and newNode.next set10 -> 10 (circular)
6Create new node with value 201 nodenewNode created10 -> 10 (circular)
7Check if head is null1 nodehead not null10 -> 10 (circular)
8Traverse to last node1 nodetemp moves to 1010 -> 10 (circular)
9Set last node.next = newNode2 nodes10.next -> newNode10 -> 20 -> ?
10Set newNode.next = head2 nodesnewNode.next -> head10 -> 20 -> 10 (circular)
11Add node complete2 nodespointers updated10 -> 20 -> 10 (circular)
12Create new node with value 302 nodesnewNode created10 -> 20 -> 10 (circular)
13Check if head is null2 nodeshead not null10 -> 20 -> 10 (circular)
14Traverse to last node2 nodestemp moves 10 -> 2010 -> 20 -> 10 (circular)
15Set last node.next = newNode3 nodes20.next -> newNode10 -> 20 -> 30 -> ?
16Set newNode.next = head3 nodesnewNode.next -> head10 -> 20 -> 30 -> 10 (circular)
17Add node complete3 nodespointers updated10 -> 20 -> 30 -> 10 (circular)
18Stop3 nodesAll nodes linked circularly10 -> 20 -> 30 -> 10 (circular)
💡 All nodes linked so last node points back to head, forming circular list.
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 9After Step 12After Step 15Final
headnullNode(10)Node(10)Node(10)Node(10)Node(10)Node(10)
newNodenullNode(10)Node(20)Node(20)Node(30)Node(30)Node(30)
tempnullnullnullNode(10)nullNode(20)Node(20)
Key Moments - 3 Insights
Why do we set newNode.next = head when adding the first node?
Because in a circular list, even a single node points to itself to form the circle. See step 4 in execution_table where newNode.next points to head.
Why do we traverse until temp.next == head instead of temp == null?
Because in a circular list, the last node points back to head, not null. So we stop when temp.next is head to find the last node. See step 8 and 14.
What happens if we forget to set last node.next = newNode?
The new node won't be linked into the list, breaking the circle. The list would be incomplete. See step 9 and 15 where this pointer update is crucial.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what does newNode.next point to?
Anull
BnewNode itself
Chead node
Dlast node
💡 Hint
Check Pointer Changes column at step 4 in execution_table.
At which step does the list first have two nodes linked circularly?
AStep 5
BStep 10
CStep 15
DStep 17
💡 Hint
Look for when two nodes appear in Nodes in List and Visual State columns.
If head was null at step 7, what would happen next?
ASet head = newNode
BCreate new node again
CTraverse to last node
DStop execution
💡 Hint
See step 2 and 3 for what happens when head is null.
Concept Snapshot
Create a new node.
If list empty: head = new node, newNode.next = head (points to itself).
Else: traverse to last node (where next points to head).
Set last node.next = newNode.
Set newNode.next = head to maintain circular link.
Result: last node points to new node, new node points to head, forming a circle.
Full Transcript
To create a circular singly linked list, first create a new node with the given value. If the list is empty (head is null), set head to this new node and point newNode.next back to head, making it circular with one node. If the list is not empty, traverse from head until you find the last node whose next points back to head. Then set this last node's next to the new node, and set newNode.next to head. This maintains the circular structure. The execution table shows each step adding nodes 10, 20, and 30, updating pointers accordingly. The variable tracker shows how head, newNode, and temp change during the process. Key moments clarify why newNode.next points to head and why traversal stops at temp.next == head. The visual quiz tests understanding of pointer assignments and list state at different steps.

Practice

(1/5)
1. What is the key difference between a circular singly linked list and a regular singly linked list?
easy
A. The last node points to null in a circular singly linked list.
B. Nodes have two pointers in a circular singly linked list.
C. The last node points back to the first node in a circular singly linked list.
D. Nodes are stored in an array in a circular singly linked list.

Solution

  1. Step 1: Understand the node connection in a regular singly linked list

    In a regular singly linked list, the last node's next pointer is null, marking the end.
  2. Step 2: Understand the node connection in a circular singly linked list

    In a circular singly linked list, the last node's next pointer points back to the first node, forming a circle.
  3. Final Answer:

    The last node points back to the first node in a circular singly linked list. -> Option C
  4. Quick Check:

    Last node next = first node in circular list [OK]
Hint: Remember: circular means last node loops to first [OK]
Common Mistakes:
  • Thinking last node points to null in circular list
  • Confusing circular with doubly linked list
  • Assuming nodes have two pointers in circular singly linked list
2. Which of the following is the correct way to initialize a circular singly linked list with one node in C#?
easy
A. Node head = new Node(5); head.next = head;
B. Node head = new Node(5); head.next = null;
C. Node head = null; head.next = head;
D. Node head = new Node(5); head = null;

Solution

  1. Step 1: Create a new node with value 5

    Node head = new Node(5); creates a node with data 5.
  2. Step 2: Point the node's next to itself to form a circular link

    head.next = head; makes the list circular with one node.
  3. Final Answer:

    Node head = new Node(5); head.next = head; -> Option A
  4. Quick Check:

    Single node next points to itself [OK]
Hint: Single node circular list points next to itself [OK]
Common Mistakes:
  • Setting next to null instead of self
  • Assigning null to head after creation
  • Trying to set next on a null head
3. What will be the output of the following code snippet that creates a circular singly linked list with nodes 1, 2, 3 and prints them once?
class Node {
  public int data;
  public Node next;
  public Node(int d) { data = d; next = null; }
}

Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.next = third;
third.next = head; // circular link

Node temp = head;
do {
  Console.Write(temp.data + " -> ");
  temp = temp.next;
} while (temp != head);
Console.WriteLine("null");
medium
A. 1 -> 2 -> null
B. 1 -> 2 -> 3 -> 1 -> 2 -> 3 -> null
C. 1 -> 2 -> 3 -> 3 -> null
D. 1 -> 2 -> 3 -> null

Solution

  1. Step 1: Create nodes and link them circularly

    Nodes 1, 2, 3 are linked so that third.next points back to head, forming a circle.
  2. Step 2: Traverse and print nodes once using do-while loop

    The loop prints data starting at head and stops when temp returns to head, printing 1, 2, 3 once.
  3. Final Answer:

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

    Loop prints each node once before stopping [OK]
Hint: do-while stops after one full circle [OK]
Common Mistakes:
  • Using while loop without do causes skipping first node
  • Printing nodes infinitely without stop condition
  • Confusing circular list with linear list output
4. Identify the error in this code snippet that tries to insert a new node with value 4 at the end of a circular singly linked list:
Node newNode = new Node(4);
Node temp = head;
while (temp.next != null) {
  temp = temp.next;
}
temp.next = newNode;
newNode.next = head;
medium
A. newNode.next should be null, not head.
B. The while loop condition should check for temp.next != head, not null.
C. temp should be initialized to null before the loop.
D. The newNode should be created after the loop.

Solution

  1. Step 1: Understand circular list's last node pointer

    In a circular singly linked list, the last node's next points to head, never null.
  2. Step 2: Correct the loop condition to find last node

    The loop should check temp.next != head to stop at the last node, not temp.next != null.
  3. Final Answer:

    The while loop condition should check for temp.next != head, not null. -> Option B
  4. Quick Check:

    Last node next = head, not null [OK]
Hint: In circular list, last node points to head, not null [OK]
Common Mistakes:
  • Using null check in circular list traversal
  • Setting newNode.next to null breaking circularity
  • Initializing temp incorrectly
5. You want to create a circular singly linked list from an array of integers [10, 20, 30, 40]. Which approach correctly creates the circular list and returns the head node?
public Node CreateCircularList(int[] arr) {
  if (arr.Length == 0) return null;
  Node head = new Node(arr[0]);
  Node current = head;
  for (int i = 1; i < arr.Length; i++) {
    current.next = new Node(arr[i]);
    current = current.next;
  }
  // What should be here?
}
hard
A. current.next = head; return head;
B. current.next = null; return head;
C. head.next = current; return current;
D. return current;

Solution

  1. Step 1: Create nodes from array and link linearly

    Loop creates nodes for each array element and links them in order.
  2. Step 2: Make the list circular by linking last node to head

    Setting current.next = head closes the loop, making the list circular.
  3. Step 3: Return the head node

    Return head to provide access to the circular list.
  4. Final Answer:

    current.next = head; return head; -> Option A
  5. Quick Check:

    Last node points to head to form circle [OK]
Hint: Close circle by linking last node to head [OK]
Common Mistakes:
  • Leaving last node next as null
  • Returning last node instead of head
  • Linking head.next to last node instead of last.next to head
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