Bird
Raised Fist0
DSA Javaprogramming~10 mins

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

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

// Insert node at end of circular list
void insert(int data) {
  Node newNode = new Node(data);
This code creates a new node and inserts it at the end of a circular singly linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=10EmptynewNode.next = nullnull
2Check if list is empty (head == null)EmptyNo pointer changesnull
3Set head = newNode1 nodehead -> newNode10 -> null
4Set newNode.next = head (self loop)1 nodenewNode.next -> head10 -> (points back to 10)
5Insert new node with data=201 nodenewNode2.next = null10 -> (points back to 10)
6Traverse to last node (node with data=10)1 nodeNo pointer changes10 -> (points back to 10)
7Set last node.next = newNode22 nodes10.next -> newNode210 -> 20
8Set newNode2.next = head2 nodesnewNode2.next -> head10 -> 20 -> (points back to 10)
9Insert new node with data=302 nodesnewNode3.next = null10 -> 20 -> (points back to 10)
10Traverse to last node (node with data=20)2 nodesNo pointer changes10 -> 20 -> (points back to 10)
11Set last node.next = newNode33 nodes20.next -> newNode310 -> 20 -> 30
12Set newNode3.next = head3 nodesnewNode3.next -> head10 -> 20 -> 30 -> (points back to 10)
13End of insertions3 nodesNo pointer changes10 -> 20 -> 30 -> (points back to 10)
💡 All nodes inserted; last node points back to head to maintain circularity.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 7After Step 8After Step 11After Step 12Final
headnullNode(10)Node(10)Node(10)Node(10)Node(10)Node(10)Node(10)
newNodenullNode(10)Node(10)Node(10)Node(10)nullnullnull
newNode2nullnullnullNode(20)Node(20)Node(20)Node(20)Node(20)
newNode3nullnullnullnullnullNode(30)Node(30)Node(30)
lastNodenullnullNode(10)Node(10)Node(20)Node(20)Node(20)Node(20)
Key Moments - 3 Insights
Why does the new node point back to head instead of null?
Because in a circular singly linked list, the last node must point back to the head to form a circle. See execution_table steps 4, 8, and 12 where newNode.next is set to head.
How do we find the last node to insert a new node?
We start at head and move through nodes until we find one whose next points back to head. This is shown in steps 6 and 10 where traversal finds the last node.
What happens if the list is empty when inserting the first node?
If the list is empty (head == null), we set head to the new node and point newNode.next to head itself, creating a single-node circular list. See steps 2 to 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what does newNode.next point to?
AIt points to head (itself)
BIt points to null
CIt points to the next new node
DIt points to the last node
💡 Hint
Check the Pointer Changes and Visual State columns at step 4 in execution_table.
At which step does the list first have two nodes linked circularly?
AStep 4
BStep 7
CStep 8
DStep 10
💡 Hint
Look for when the second node's next points back to head in execution_table.
If we skip setting lastNode.next = newNode, what happens to the list?
AThe list remains circular
BThe list becomes linear and breaks circularity
CThe new node is not linked and lost
DThe head pointer changes
💡 Hint
Refer to Pointer Changes in steps 7 and 11 where lastNode.next is updated.
Concept Snapshot
Create a new node with data.
If list empty: head = new node; newNode.next = head (self loop).
Else: traverse to last node (node.next == head).
Set last node.next = new node.
Set new node.next = head to maintain circular link.
Result: last node points back to head forming a circle.
Full Transcript
To create a circular singly linked list, start by making a new node with the given data. If the list is empty, set the head to this new node and point the new node's next to itself, making a single-node circle. If the list is not empty, find the last node by traversing from head until you find a node whose next points back to head. Then, set this last node's next to the new node, and set the new node's next to head. This keeps the list circular. Each insertion updates the pointers to maintain the circle. The execution table shows each step with pointer changes and the visual state of the list. Key moments include understanding why new nodes point back to head and how to find the last node. The visual quiz tests understanding of these pointer updates and circular structure.

Practice

(1/5)
1. What is the main difference between a circular singly linked list and a regular singly linked list?
easy
A. In a circular singly linked list, nodes have two pointers instead of one.
B. In a circular singly linked list, nodes are stored in an array.
C. In a circular singly linked list, the last node points back to the first node.
D. In a circular singly linked list, the first node points to null.

Solution

  1. Step 1: Understand the structure of a regular singly linked list

    A regular singly linked list has nodes where each node points to the next node, and the last node points to null.
  2. Step 2: Understand the structure of a circular singly linked list

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

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

    Last node points to first node = B [OK]
Hint: Remember: circular means last node links back to start [OK]
Common Mistakes:
  • Thinking circular means nodes have two pointers
  • Confusing circular list with doubly linked list
  • Assuming last node points to null
2. Which of the following is the correct way to create a new node in Java for a circular singly linked list?
easy
A. Node newNode = new Node(); newNode.next = newNode;
B. Node newNode = new Node(data); newNode.next = null;
C. Node newNode = new Node(); newNode.data = data; newNode.next = null;
D. Node newNode = new Node(data); newNode.next = newNode;

Solution

  1. Step 1: Understand node creation for circular singly linked list

    When creating a single node in a circular singly linked list, its next pointer should point to itself to form a circle.
  2. Step 2: Check each option

    Node newNode = new Node(data); newNode.next = newNode; sets newNode.next = newNode, which correctly points the node to itself. Other options set next to null or do not initialize properly.
  3. Final Answer:

    Node newNode = new Node(data); newNode.next = newNode; -> Option D
  4. Quick Check:

    Node points to itself when alone = A [OK]
Hint: Single node's next points to itself in circular list [OK]
Common Mistakes:
  • Setting next to null for single node
  • Not initializing next pointer
  • Confusing syntax for node creation
3. What will be the output of the following code snippet that creates a circular singly linked list with two nodes and prints their data?
class Node {
  int data;
  Node next;
  Node(int d) { data = d; next = null; }
}

public class Test {
  public static void main(String[] args) {
    Node head = new Node(10);
    Node second = new Node(20);
    head.next = second;
    second.next = head;

    System.out.print(head.data + " -> " + head.next.data + " -> " + head.next.next.data);
  }
}
medium
A. 10 -> 20 -> 10
B. 10 -> 20 -> null
C. 10 -> 20 -> 20
D. Compilation error

Solution

  1. Step 1: Trace the node connections

    head.data = 10, head.next = second (data 20), second.next = head (data 10), forming a circle.
  2. Step 2: Evaluate the print statement

    head.data = 10, head.next.data = 20, head.next.next.data = second.next.data = head.data = 10.
  3. Final Answer:

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

    Circle repeats first node data = 10 -> 20 -> 10 [OK]
Hint: Follow next pointers carefully to see the cycle [OK]
Common Mistakes:
  • Assuming next.next is null
  • Confusing node data values
  • Expecting compilation error
4. Identify the error in the following code that tries to insert a new node at the end of a circular singly linked list:
public void insertEnd(Node head, int data) {
  Node newNode = new Node(data);
  if (head == null) {
    head = newNode;
    newNode.next = head;
  } else {
    Node temp = head;
    while (temp.next != null) {
      temp = temp.next;
    }
    temp.next = newNode;
    newNode.next = head;
  }
}
medium
A. The while loop condition should check for temp.next != head instead of null.
B. The newNode.next should be set to null instead of head.
C. The head should be updated to newNode after insertion.
D. The method should return the new head node.

Solution

  1. Step 1: Understand circular list traversal

    In a circular singly linked list, the last node's next points to head, not null.
  2. Step 2: Analyze the while loop condition

    The loop checks temp.next != null, which never happens in circular list; it should check temp.next != head to find the last node.
  3. Final Answer:

    The while loop condition should check for temp.next != head instead of null. -> Option A
  4. Quick Check:

    Loop must stop at last node pointing to head = A [OK]
Hint: In circular list, last node points to head, not null [OK]
Common Mistakes:
  • Using null to detect end in circular list
  • Not updating newNode.next correctly
  • Ignoring head update when list is empty
5. You want to create a circular singly linked list from an array of integers {5, 10, 15} by inserting nodes at the end. Which sequence of next pointers correctly represents the final circular list?
hard
A. 15 -> 10 -> 5 -> 15
B. 5 -> 10 -> 15 -> 5
C. 5 -> 10 -> 15 -> null
D. 5 -> 15 -> 10 -> 5

Solution

  1. Step 1: Insert nodes at end in given order

    Insert 5 first, then 10 at end, then 15 at end, preserving order: 5, 10, 15.
  2. Step 2: Set last node's next to head

    After insertion, last node (15) points back to head (5), making the list circular.
  3. Final Answer:

    5 -> 10 -> 15 -> 5 -> Option B
  4. Quick Check:

    Order preserved, last points to first = C [OK]
Hint: Insert at end keeps order; last node points to first [OK]
Common Mistakes:
  • Assuming last node points to null
  • Reversing insertion order
  • Mixing node data order
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