📖 Scenario: Imagine you are building a music playlist app where songs play in a loop. To manage this, you will create a circular singly linked list where the last song points back to the first song, so the playlist repeats endlessly.
🎯 Goal: Build a circular singly linked list in Java that holds three songs. Each node should contain the song name and a reference to the next song. The last song should link back to the first song to form a circle.
📋 What You'll Learn
Create a Node class with a String field songName and a Node field next.
Create three nodes with song names exactly: "Song A", "Song B", and "Song C".
Link the nodes so that Song A points to Song B, Song B points to Song C, and Song C points back to Song A.
Create a variable head that points to the first node (Song A).
Print the circular list by traversing starting from head and stopping after printing all three songs.
💡 Why This Matters
🌍 Real World
Circular linked lists are used in music players, round-robin schedulers, and games where looping through items endlessly is needed.
💼 Career
Understanding circular linked lists helps in software development roles that involve data structure design, system programming, and real-time applications.
Progress0 / 4 steps
1
Create the Node class and three nodes
Create a Node class with a String field songName and a Node field next. Then create three Node objects named node1, node2, and node3 with song names "Song A", "Song B", and "Song C" respectively.
DSA Java
Hint
Define a class Node with two fields: songName and next. Then create three nodes with the exact song names.
2
Link the nodes to form a circular list
Link the nodes so that node1.next points to node2, node2.next points to node3, and node3.next points back to node1. Also, create a variable head that points to node1.
DSA Java
Hint
Set the next of each node to the next node, and make the last node point back to the first. Then assign head to the first node.
3
Traverse the circular list and print song names
Use a Node variable current starting at head. Use a for loop that runs exactly 3 times to print the songName of each node. Move current to current.next in each iteration.
DSA Java
Hint
Use a for loop that runs 3 times. Print current.songName and then move current to current.next.
4
Print the circular singly linked list
Run the program to print the song names in order: Song A, Song B, and Song C.
DSA Java
Hint
Run the program. It should print the three song names in order, each on its own line.
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
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.
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.
Final Answer:
In a circular singly linked list, the last node points back to the first node. -> Option C
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
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.
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.
Final Answer:
Node newNode = new Node(data); newNode.next = newNode; -> Option D
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
Step 1: Trace the node connections
head.data = 10, head.next = second (data 20), second.next = head (data 10), forming a circle.
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
Step 1: Understand circular list traversal
In a circular singly linked list, the last node's next points to head, not null.
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.
Final Answer:
The while loop condition should check for temp.next != head instead of null. -> Option A
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
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.
Step 2: Set last node's next to head
After insertion, last node (15) points back to head (5), making the list circular.
Final Answer:
5 -> 10 -> 15 -> 5 -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.