📖 Scenario: You are managing a circular queue of tasks where the last task points back to the first. You want to add a new task at the end of this circular list.
🎯 Goal: Build a circular linked list of tasks and insert a new task at the end, then print the list to verify the insertion.
📋 What You'll Learn
Create a circular linked list with three nodes named Task1, Task2, and Task3
Create a variable called newTask with the value "Task4"
Write a function called insertAtEnd that inserts a new node with the given task name at the end of the circular linked list
Print the circular linked list starting from the head node, showing all tasks in order
💡 Why This Matters
🌍 Real World
Circular linked lists are useful in scheduling tasks where the last task loops back to the first, such as in round-robin task management.
💼 Career
Understanding circular linked lists helps in roles involving system programming, embedded systems, and real-time scheduling algorithms.
Progress0 / 4 steps
1
Create initial circular linked list
Create a struct type called Node with fields task string and next *Node. Then create three nodes named task1, task2, and task3 with tasks "Task1", "Task2", and "Task3" respectively. Link them in a circular way so that task3.next points back to task1. Finally, create a variable head that points to task1.
DSA Go
Hint
Define the Node struct first. Then create nodes with &Node{task: "TaskName"}. Link them by setting the next pointers. Make sure the last node points back to the first.
2
Create new task variable
Create a variable called newTask and set it to the string value "Task4".
DSA Go
Hint
Just create a string variable named newTask and assign it the value "Task4".
3
Write insertAtEnd function
Write a function called insertAtEnd that takes two parameters: head *Node and task string. It should create a new node with the given task and insert it at the end of the circular linked list. The function should return the updated head *Node. Use a for loop to find the last node (the one whose next points to head). Then update pointers to insert the new node at the end.
DSA Go
Hint
Create a new node with the task. If the list is empty, point new node to itself and return it. Otherwise, loop until you find the last node (where next points to head). Insert the new node after the last node and point new node's next to head.
4
Print circular linked list
Write code to print the tasks in the circular linked list starting from head. Use a for loop to traverse nodes until you reach head again. Print each task followed by " -> ". After the loop, print "(back to head)".
DSA Go
Hint
Start from head and print each node's task. Stop when you reach head again to avoid infinite loop.
Practice
(1/5)
1. What happens when you insert a new node at the end of an empty circular linked list?
easy
A. The new node points to itself, becoming the only node in the list
B. The new node points to null, ending the list
C. The list remains empty because insertion is not possible
D. The new node points to the head, but the list has multiple nodes
Solution
Step 1: Understand empty circular linked list
When the list is empty, there is no node, so the new node must point to itself to maintain circularity.
Step 2: Insert new node and update pointers
The new node's next pointer is set to itself, making it the only node and the head of the list.
Final Answer:
The new node points to itself, becoming the only node in the list -> Option A
Quick Check:
Empty list insertion = single node pointing to itself [OK]
Hint: New node points to itself if list is empty [OK]
Common Mistakes:
Setting new node's next to null instead of itself
Not updating head pointer
Assuming list remains empty after insertion
2. Which of the following Go code snippets correctly inserts a new node at the end of a circular linked list?
easy
A. newNode.next = nil
last.next = newNode
last = newNode
B. newNode.next = head
last.next = newNode
newNode = last
C. newNode.next = head
last.next = newNode
last = newNode
D. newNode.next = head
newNode.next = last
last = newNode
Solution
Step 1: Set new node's next to head
In circular linked list, new node's next must point to head to maintain circularity.
Step 2: Update last node's next and last pointer
Last node's next points to new node, and last pointer updates to new node.
Final Answer:
newNode.next = head; last.next = newNode; last = newNode -> Option C
Quick Check:
Correct pointer updates maintain circular list [OK]
Hint: New node next = head; last next = new node; update last [OK]
A. newNode.next should point to itself when list is empty, not nil
B. last.next should be set to nil instead of newNode
C. Function should return last, not newNode
D. Data field of newNode is not initialized
Solution
Step 1: Check empty list insertion
When list is empty, newNode.next must point to itself to maintain circularity, not nil.
Step 2: Identify incorrect pointer assignment
Setting newNode.next to nil breaks circular linked list property causing runtime errors.
Final Answer:
newNode.next should point to itself when list is empty, not nil -> Option A
Quick Check:
Empty list node next = itself [OK]
Hint: Empty list node next must point to itself, never nil [OK]
Common Mistakes:
Setting next to nil breaks circularity
Returning newNode instead of last can confuse list end
Not initializing data field properly
5. You have a circular linked list with nodes 10 -> 20 -> 30 -> (back to 10). You want to insert 40 at the end. After insertion, which node should the last pointer refer to, and what will be the list order when printed starting from last.next?
Choose the correct pair.
hard
A. Last points to node with data 30; list order: 40 -> 10 -> 20 -> 30 -> null
B. Last points to node with data 40; list order: 10 -> 20 -> 30 -> 40 -> null
C. Last points to node with data 40; list order: 40 -> 10 -> 20 -> 30 -> null
D. Last points to node with data 30; list order: 10 -> 20 -> 30 -> 40 -> null
Solution
Step 1: Insert new node at end and update last pointer
New node 40 is added after last (30), so last pointer updates to new node 40.
Step 2: Print list starting from last.next
last.next points to head (10), so printing order is 10 -> 20 -> 30 -> 40 -> null.
Final Answer:
Last points to node with data 40; list order: 10 -> 20 -> 30 -> 40 -> null -> Option B
Quick Check:
Last updated to new end node; print starts at last.next [OK]
Hint: Last always points to newest end node; print starts at last.next [OK]
Common Mistakes:
Not updating last pointer after insertion
Printing starting from last instead of last.next
Confusing list order after insertion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.