Bird
Raised Fist0
DSA Goprogramming~10 mins

Insert at End of Circular Linked List in DSA Go - Execution Trace

Choose your learning style10 modes available
Concept Flow - Insert at End of Circular Linked List
Create new node
↓
Is list empty?
↓
Point new node to itself
↓
Set head = new node
↓
Traverse until current.next == head
↓
Set current.next = new node
↓
Set new node.next = head
↓
Done
Insert a new node by checking if the list is empty, then either initialize or traverse to last node and link new node at end.
Execution Sample
DSA Go
func insertEnd(head *Node, data int) *Node {
  newNode := &Node{data: data}
  if head == nil {
    newNode.next = newNode
    return newNode
  }
  current := head
  for current.next != head {
    current = current.next
  }
  current.next = newNode
  newNode.next = head
  return head
}
This code inserts a new node with given data 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, next=nullā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head ↑ last node newNode: data=40, next=āˆ…
2Check if list is empty (head == nil)10 -> 20 -> 30 -> (back to 10)No changeList not empty, head points to 10
3Set current = head10 -> 20 -> 30 -> (back to 10)current points to 10current → 10
4Traverse until current.next == head10 -> 20 -> 30 -> (back to 10)current moves: 10 → 20current → 20
5Traverse until current.next == head10 -> 20 -> 30 -> (back to 10)current moves: 20 → 30current → 30
6current.next == head (30.next == 10), stop traversal10 -> 20 -> 30 -> (back to 10)No changecurrent at last node 30
7Set current.next = newNode10 -> 20 -> 30 -> 40 -> (back to 10)30.next → newNodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ data:40│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head newNode
8Set newNode.next = head10 -> 20 -> 30 -> 40 -> (back to 10)newNode.next → head (10)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ data:40│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head newNode ↑ back to head
9Return head10 -> 20 -> 30 -> 40 -> (back to 10)No changeList updated with new node at end
šŸ’” Traversal stops when current.next == head, new node linked at end, circular link maintained
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 7After Step 8Final
headpoints to node 10points to node 10points to node 10points to node 10points to node 10points to node 10points to node 10
currentnilpoints to node 10points to node 20points to node 30points to node 30points to node 30points to node 30
newNodenilcreated with data=40samesamesamesamesame
Key Moments - 3 Insights
Why do we check current.next == head instead of current == nil?
Because in a circular linked list, the last node points back to head, not nil. So traversal stops when current.next == head (see execution_table step 6).
What happens if the list is empty when inserting?
If head is nil, new node points to itself and becomes head (execution_table step 2). This creates a single-node circular list.
Why do we set newNode.next = head after linking it at the end?
To maintain the circular structure, newNode must point back to head (execution_table step 8), closing the loop.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does 'current' point to after step 5?
ANode with data 30
BNode with data 20
CNode with data 10
DNew node with data 40
šŸ’” Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 5 in execution_table.
At which step does the new node get linked into the list?
AStep 4
BStep 7
CStep 2
DStep 9
šŸ’” Hint
Look for the step where 'current.next' changes to point to newNode in execution_table.
If the list was empty initially, what would newNode.next point to after insertion?
Anil
Bhead (newNode itself)
Cnext node if any
Dprevious node
šŸ’” Hint
Refer to execution_table step 2 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 (current.next == head)
- Set last node's next to newNode
- Set newNode.next to head
- Maintains circular link
Full Transcript
To insert at the end of a circular linked list, first create a new node. If the list is empty (head is nil), point the new node's next to itself and set head to this new node, forming a single-node circle. If the list is not empty, start at head and traverse nodes until you find the last node whose next points back to head. Then, set this last node's next pointer to the new node. Finally, set the new node's next pointer to head to maintain the circular structure. Return the head pointer unchanged. This process ensures the new node is added at the end and the list remains circular.

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

  1. 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.
  2. 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.
  3. Final Answer:

    The new node points to itself, becoming the only node in the list -> Option A
  4. 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

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

    In circular linked list, new node's next must point to head to maintain circularity.
  2. 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.
  3. Final Answer:

    newNode.next = head; last.next = newNode; last = newNode -> Option C
  4. Quick Check:

    Correct pointer updates maintain circular list [OK]
Hint: New node next = head; last next = new node; update last [OK]
Common Mistakes:
  • Setting newNode.next to nil breaks circularity
  • Not updating last pointer to new node
  • Incorrect pointer assignments causing runtime errors
3. Given the following Go code for inserting at the end of a circular linked list, what will be the printed list after inserting 4?
type Node struct {
  data int
  next *Node
}

func insertEnd(last *Node, data int) *Node {
  newNode := &Node{data: data}
  if last == nil {
    newNode.next = newNode
    return newNode
  }
  newNode.next = last.next
  last.next = newNode
  return newNode
}

func printList(last *Node) {
  if last == nil {
    fmt.Println("List is empty")
    return
  }
  temp := last.next
  for {
    fmt.Printf("%d -> ", temp.data)
    temp = temp.next
    if temp == last.next {
      break
    }
  }
  fmt.Println("null")
}

func main() {
  var last *Node
  last = insertEnd(last, 1)
  last = insertEnd(last, 2)
  last = insertEnd(last, 3)
  last = insertEnd(last, 4)
  printList(last)
}
medium
A. 1 -> 2 -> 3 -> null
B. 4 -> 1 -> 2 -> 3 -> null
C. 2 -> 3 -> 4 -> 1 -> null
D. 1 -> 2 -> 3 -> 4 -> null

Solution

  1. Step 1: Trace insertions in order

    Insert 1: list is empty, new node points to itself. Insert 2: new node added after last, last updated. Insert 3 and 4 similarly.
  2. Step 2: Print list starting from last.next

    Printing starts at first node (last.next), so order is 1 -> 2 -> 3 -> 4 -> null.
  3. Final Answer:

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

    Insertion at end maintains order [OK]
Hint: Print starts at last.next, so order is insertion order [OK]
Common Mistakes:
  • Assuming last node is printed first
  • Not updating last pointer after insertion
  • Confusing circular list print start point
4. The following Go function is intended to insert a new node at the end of a circular linked list. What is the bug?
func insertEnd(last *Node, data int) *Node {
  newNode := &Node{data: data}
  if last == nil {
    newNode.next = nil
    return newNode
  }
  newNode.next = last.next
  last.next = newNode
  return newNode
}
medium
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

  1. Step 1: Check empty list insertion

    When list is empty, newNode.next must point to itself to maintain circularity, not nil.
  2. Step 2: Identify incorrect pointer assignment

    Setting newNode.next to nil breaks circular linked list property causing runtime errors.
  3. Final Answer:

    newNode.next should point to itself when list is empty, not nil -> Option A
  4. 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

  1. 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.
  2. Step 2: Print list starting from last.next

    last.next points to head (10), so printing order is 10 -> 20 -> 30 -> 40 -> null.
  3. Final Answer:

    Last points to node with data 40; list order: 10 -> 20 -> 30 -> 40 -> null -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard