Bird
Raised Fist0
DSA Goprogramming~20 mins

Delete a Node from Circular Linked List in DSA Go - Practice Problems & Challenges

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

import "fmt"

type Node struct {
    data int
    next *Node
}

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

func deleteNode(head *Node, key int) *Node {
    if head == nil {
        return nil
    }
    curr := head
    prev := head

    // Find last node to help with deletion
    for prev.next != head {
        prev = prev.next
    }

    // If list has only one node
    if head.data == key && head.next == head {
        return nil
    }

    // If head needs to be deleted
    if head.data == key {
        prev.next = head.next
        head = head.next
        return head
    }

    // Delete non-head node
    curr = head.next
    prev = head
    for curr != head {
        if curr.data == key {
            prev.next = curr.next
            return head
        }
        prev = curr
        curr = curr.next
    }
    return head
}

func main() {
    // Create circular linked list: 1 -> 2 -> 3 -> 4 -> 1
    n1 := &Node{data: 1}
    n2 := &Node{data: 2}
    n3 := &Node{data: 3}
    n4 := &Node{data: 4}
    n1.next = n2
    n2.next = n3
    n3.next = n4
    n4.next = n1

    head := n1
    head = deleteNode(head, 3)
    printList(head)
}
A1 -> 2 -> 3 -> 4 -> null
B1 -> 2 -> 4 -> null
C2 -> 4 -> 1 -> null
D1 -> 4 -> null
Attempts:
2 left
💡 Hint
Remember to update the links so the node with value 3 is removed and the list remains circular.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the head node
What is the printed state of the circular linked list after deleting the node with value 1 (the head)?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    next *Node
}

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

func deleteNode(head *Node, key int) *Node {
    if head == nil {
        return nil
    }
    curr := head
    prev := head

    for prev.next != head {
        prev = prev.next
    }

    if head.data == key && head.next == head {
        return nil
    }

    if head.data == key {
        prev.next = head.next
        head = head.next
        return head
    }

    curr = head.next
    prev = head
    for curr != head {
        if curr.data == key {
            prev.next = curr.next
            return head
        }
        prev = curr
        curr = curr.next
    }
    return head
}

func main() {
    n1 := &Node{data: 1}
    n2 := &Node{data: 2}
    n3 := &Node{data: 3}
    n4 := &Node{data: 4}
    n1.next = n2
    n2.next = n3
    n3.next = n4
    n4.next = n1

    head := n1
    head = deleteNode(head, 1)
    printList(head)
}
A3 -> 4 -> 2 -> null
B1 -> 2 -> 3 -> 4 -> null
C2 -> 3 -> 4 -> null
D4 -> 2 -> 3 -> null
Attempts:
2 left
💡 Hint
Deleting the head means the new head is the next node, and the last node points to it.
🔧 Debug
advanced
2:00remaining
Identify the error in this deletion code
What error will this code produce when trying to delete a node from a circular linked list?
DSA Go
func deleteNode(head *Node, key int) *Node {
    if head == nil {
        return nil
    }
    curr := head
    prev := head

    // Incorrect loop condition
    for prev.next != nil {
        prev = prev.next
    }

    if head.data == key && head.next == head {
        return nil
    }

    if head.data == key {
        prev.next = head.next
        head = head.next
        return head
    }

    curr = head.next
    prev = head
    for curr != head {
        if curr.data == key {
            prev.next = curr.next
            return head
        }
        prev = curr
        curr = curr.next
    }
    return head
}
AInfinite loop
BNil pointer dereference runtime error
CCorrect deletion with no error
DCompilation error: invalid syntax
Attempts:
2 left
💡 Hint
Check the loop condition for finding the last node in a circular linked list.
❓ Predict Output
advanced
2:00remaining
Output after deleting the only node in the list
What is the printed output after deleting the only node (value 10) in a circular linked list?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    next *Node
}

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

func deleteNode(head *Node, key int) *Node {
    if head == nil {
        return nil
    }
    curr := head
    prev := head

    for prev.next != head {
        prev = prev.next
    }

    if head.data == key && head.next == head {
        return nil
    }

    if head.data == key {
        prev.next = head.next
        head = head.next
        return head
    }

    curr = head.next
    prev = head
    for curr != head {
        if curr.data == key {
            prev.next = curr.next
            return head
        }
        prev = curr
        curr = curr.next
    }
    return head
}

func main() {
    n1 := &Node{data: 10}
    n1.next = n1

    head := n1
    head = deleteNode(head, 10)
    printList(head)
}
A10 -> 10 -> null
B10 -> null
CRuntime error: nil pointer dereference
DList is empty
Attempts:
2 left
💡 Hint
Deleting the only node should result in an empty list.
🧠 Conceptual
expert
2:00remaining
Why is it necessary to find the last node when deleting the head in a circular linked list?
In a circular linked list, when deleting the head node, why must we find the last node before updating the head?
ATo update the last node's next pointer to the new head, maintaining the circular link
BTo reverse the list before deleting the head
CTo find the node with the maximum value before deletion
DTo free the memory of the last node before deleting the head
Attempts:
2 left
💡 Hint
Think about what keeps the list circular after the head changes.

Practice

(1/5)
1. What happens when you delete the only node in a circular linked list?
easy
A. The list still contains the deleted node.
B. The list becomes empty (head points to nil).
C. The list forms a linear linked list.
D. The list throws an error automatically.

Solution

  1. Step 1: Understand single-node circular list

    A circular linked list with one node points to itself.
  2. Step 2: Delete the only node

    Removing this node means no nodes remain, so head becomes nil.
  3. Final Answer:

    The list becomes empty (head points to nil). -> Option B
  4. Quick Check:

    Single node deleted = empty list [OK]
Hint: Deleting last node empties the list, head = nil [OK]
Common Mistakes:
  • Assuming list still has nodes after deleting the only node
  • Not updating head to nil
  • Confusing circular with linear list after deletion
2. Which Go syntax correctly moves the head pointer when deleting the head node from a circular linked list?
easy
A. head.next = head
B. head = nil.next
C. head = head.next
D. head = head.prev

Solution

  1. Step 1: Identify head deletion logic

    Deleting head means moving head pointer to next node.
  2. Step 2: Check syntax correctness

    Assigning head = head.next correctly moves head to next node.
  3. Final Answer:

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

    Move head to next node = head = head.next [OK]
Hint: Move head pointer to next node to delete head [OK]
Common Mistakes:
  • Using nil.next causes runtime error
  • Assigning head.next = head does not delete node
  • Using head.prev is invalid for singly circular list
3. Given this circular linked list: 1 -> 2 -> 3 -> (back to 1), what is the list after deleting node with value 2?
medium
A. 1 -> 3 -> (back to 1)
B. 1 -> 2 -> 3 -> (back to 1)
C. 2 -> 3 -> (back to 2)
D. 3 -> 1 -> (back to 3)

Solution

  1. Step 1: Locate node with value 2

    Node 2 is between nodes 1 and 3 in the circular list.
  2. Step 2: Remove node 2 by linking 1 to 3

    Update node 1's next pointer to node 3, skipping node 2.
  3. Final Answer:

    1 -> 3 -> (back to 1) -> Option A
  4. Quick Check:

    Deleted 2, linked 1 to 3 [OK]
Hint: Link previous node to next of deleted node [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Deleting wrong node
  • Breaking circular link
4. What is wrong with this Go code snippet for deleting a node from a circular linked list?
func deleteNode(head *Node, key int) *Node {
  if head == nil {
    return nil
  }
  curr := head
  for curr.next != head && curr.next.data != key {
    curr = curr.next
  }
  if curr.next.data == key {
    curr.next = curr.next.next
  }
  return head
}
medium
A. It returns nil always.
B. It causes infinite loop always.
C. It deletes all nodes with the key.
D. It does not handle deleting the head node correctly.

Solution

  1. Step 1: Analyze deletion logic

    The code searches for node before target and skips target node.
  2. Step 2: Check head deletion case

    If the node to delete is head, head pointer is not updated, causing incorrect list.
  3. Final Answer:

    It does not handle deleting the head node correctly. -> Option D
  4. Quick Check:

    Head deletion needs head pointer update [OK]
Hint: Update head if deleting head node [OK]
Common Mistakes:
  • Ignoring head node deletion
  • Not updating head pointer
  • Assuming key always exists
5. You have a circular linked list: 10 -> 20 -> 30 -> 40 -> (back to 10). You want to delete node with value 10 (head). Which steps correctly update the list?
hard
A. Find last node (40), set its next to head.next (20), then update head to 20.
B. Set head to nil directly without updating last node.
C. Delete node 10 by setting head.next to nil.
D. Swap data of head and next node, then delete next node.

Solution

  1. Step 1: Find last node pointing to head

    Traverse list to find node 40 which points to head (10).
  2. Step 2: Update last node's next pointer

    Set node 40's next to head.next (node 20) to maintain circularity.
  3. Step 3: Update head pointer

    Set head to head.next (node 20) to delete original head.
  4. Final Answer:

    Find last node (40), set its next to head.next (20), then update head to 20. -> Option A
  5. Quick Check:

    Update last node and head to delete head node [OK]
Hint: Update last node's next and head to delete head [OK]
Common Mistakes:
  • Not updating last node's next pointer
  • Setting head to nil breaks list
  • Deleting node by setting next to nil
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