Bird
Raised Fist0
DSA Goprogramming~20 mins

Insert at End of Circular Linked List in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of inserting a node at the end of a circular linked list
What is the printed state of the circular linked list after inserting a node with value 4 at the end?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    next *Node
}

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

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

func main() {
    var head *Node
    head = insertEnd(head, 1)
    head = insertEnd(head, 2)
    head = insertEnd(head, 3)
    head = insertEnd(head, 4)
    printList(head)
}
A4 -> 1 -> 2 -> 3 -> null
B1 -> 2 -> 3 -> 4
C1 -> 2 -> 3 -> null
D1 -> 2 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Remember that in a circular linked list, the last node points back to the head.
❓ Predict Output
intermediate
2:00remaining
Output after inserting into an empty circular linked list
What is the printed state of the circular linked list after inserting a node with value 10 into an empty list?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    next *Node
}

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

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

func main() {
    var head *Node
    head = insertEnd(head, 10)
    printList(head)
}
Anull
B10 -> null
C10 -> 10 -> null
DList is empty
Attempts:
2 left
💡 Hint
When inserting into an empty circular linked list, the new node points to itself.
🔧 Debug
advanced
2:00remaining
Identify the error in this circular linked list insertion code
What error will this code produce when inserting a node at the end of a circular linked list?
DSA Go
func insertEnd(head *Node, data int) *Node {
    newNode := &Node{data: data}
    if head == nil {
        newNode.next = nil
        return newNode
    }
    temp := head
    for temp.next != head {
        temp = temp.next
    }
    temp.next = newNode
    newNode.next = head
    return head
}
ACompilation error due to missing return statement
BRuntime panic due to nil pointer dereference
CThe list will become linear, losing circularity
DInfinite loop in traversal
Attempts:
2 left
💡 Hint
Check what happens when inserting into an empty list.
❓ Predict Output
advanced
2:00remaining
Output after multiple insertions at the end of circular linked list
What is the printed state of the circular linked list after inserting nodes with values 5, 10, 15, 20 in that order?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    next *Node
}

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

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

func main() {
    var head *Node
    head = insertEnd(head, 5)
    head = insertEnd(head, 10)
    head = insertEnd(head, 15)
    head = insertEnd(head, 20)
    printList(head)
}
A5 -> 10 -> 15 -> 20 -> null
B20 -> 15 -> 10 -> 5 -> null
C5 -> 10 -> 15 -> null
D5 -> 10 -> 15 -> 20
Attempts:
2 left
💡 Hint
Nodes are inserted at the end, so order is preserved.
🧠 Conceptual
expert
2:00remaining
Why is it efficient to keep a tail pointer in a circular linked list for insertion at end?
Which is the main advantage of maintaining a tail pointer (pointer to last node) in a circular linked list when inserting at the end?
AIt allows insertion at the end in O(1) time without traversing the list
BIt makes the list sorted automatically
CIt reduces the memory usage of the list
DIt prevents the list from becoming circular
Attempts:
2 left
💡 Hint
Think about how you find the last node without a tail pointer.

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