Bird
Raised Fist0
DSA Goprogramming~20 mins

Delete Node by Value in DSA Go - Practice Problems & Challenges

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

import "fmt"

type Node struct {
    value int
    next  *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) append(val int) {
    newNode := &Node{value: val}
    if l.head == nil {
        l.head = newNode
        return
    }
    current := l.head
    for current.next != nil {
        current = current.next
    }
    current.next = newNode
}

func (l *LinkedList) deleteByValue(val int) {
    if l.head == nil {
        return
    }
    if l.head.value == val {
        l.head = l.head.next
        return
    }
    current := l.head
    for current.next != nil && current.next.value != val {
        current = current.next
    }
    if current.next != nil {
        current.next = current.next.next
    }
}

func (l *LinkedList) printList() {
    current := l.head
    for current != nil {
        fmt.Printf("%d -> ", current.value)
        current = current.next
    }
    fmt.Println("null")
}

func main() {
    list := LinkedList{}
    list.append(1)
    list.append(2)
    list.append(3)
    list.append(4)
    list.deleteByValue(3)
    list.printList()
}
A1 -> 2 -> 3 -> 4 -> null
B1 -> 2 -> 4 -> null
C2 -> 3 -> 4 -> null
D1 -> 2 -> null
Attempts:
2 left
💡 Hint
Think about how the deleteByValue function skips the node with the matching value.
❓ Predict Output
intermediate
2:00remaining
Result after deleting head node by value
What is the printed state of the linked list after deleting the node with value 1 (head node)?
DSA Go
package main

import "fmt"

type Node struct {
    value int
    next  *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) append(val int) {
    newNode := &Node{value: val}
    if l.head == nil {
        l.head = newNode
        return
    }
    current := l.head
    for current.next != nil {
        current = current.next
    }
    current.next = newNode
}

func (l *LinkedList) deleteByValue(val int) {
    if l.head == nil {
        return
    }
    if l.head.value == val {
        l.head = l.head.next
        return
    }
    current := l.head
    for current.next != nil && current.next.value != val {
        current = current.next
    }
    if current.next != nil {
        current.next = current.next.next
    }
}

func (l *LinkedList) printList() {
    current := l.head
    for current != nil {
        fmt.Printf("%d -> ", current.value)
        current = current.next
    }
    fmt.Println("null")
}

func main() {
    list := LinkedList{}
    list.append(1)
    list.append(2)
    list.append(3)
    list.deleteByValue(1)
    list.printList()
}
Anull
B1 -> 2 -> 3 -> null
C3 -> null
D2 -> 3 -> null
Attempts:
2 left
💡 Hint
Deleting the head node means the head pointer moves to the next node.
❓ Predict Output
advanced
2:00remaining
Output after deleting a non-existent value
What is the printed state of the linked list after attempting to delete a node with value 5, which does not exist in the list?
DSA Go
package main

import "fmt"

type Node struct {
    value int
    next  *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) append(val int) {
    newNode := &Node{value: val}
    if l.head == nil {
        l.head = newNode
        return
    }
    current := l.head
    for current.next != nil {
        current = current.next
    }
    current.next = newNode
}

func (l *LinkedList) deleteByValue(val int) {
    if l.head == nil {
        return
    }
    if l.head.value == val {
        l.head = l.head.next
        return
    }
    current := l.head
    for current.next != nil && current.next.value != val {
        current = current.next
    }
    if current.next != nil {
        current.next = current.next.next
    }
}

func (l *LinkedList) printList() {
    current := l.head
    for current != nil {
        fmt.Printf("%d -> ", current.value)
        current = current.next
    }
    fmt.Println("null")
}

func main() {
    list := LinkedList{}
    list.append(1)
    list.append(2)
    list.append(3)
    list.deleteByValue(5)
    list.printList()
}
A1 -> 2 -> 3 -> null
B1 -> 2 -> null
Cnull
D2 -> 3 -> null
Attempts:
2 left
💡 Hint
If the value is not found, the list remains unchanged.
❓ Predict Output
advanced
2:00remaining
Output after deleting the last node by value
What is the printed state of the linked list after deleting the node with value 4 (the last node)?
DSA Go
package main

import "fmt"

type Node struct {
    value int
    next  *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) append(val int) {
    newNode := &Node{value: val}
    if l.head == nil {
        l.head = newNode
        return
    }
    current := l.head
    for current.next != nil {
        current = current.next
    }
    current.next = newNode
}

func (l *LinkedList) deleteByValue(val int) {
    if l.head == nil {
        return
    }
    if l.head.value == val {
        l.head = l.head.next
        return
    }
    current := l.head
    for current.next != nil && current.next.value != val {
        current = current.next
    }
    if current.next != nil {
        current.next = current.next.next
    }
}

func (l *LinkedList) printList() {
    current := l.head
    for current != nil {
        fmt.Printf("%d -> ", current.value)
        current = current.next
    }
    fmt.Println("null")
}

func main() {
    list := LinkedList{}
    list.append(1)
    list.append(2)
    list.append(3)
    list.append(4)
    list.deleteByValue(4)
    list.printList()
}
A1 -> 2 -> 3 -> 4 -> null
B1 -> 2 -> null
C1 -> 2 -> 3 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting the last node means the second last node's next pointer becomes nil.
❓ Predict Output
expert
2:00remaining
Output after deleting all nodes by repeated delete calls
What is the printed state of the linked list after deleting nodes with values 1, 2, 3, and 4 in sequence?
DSA Go
package main

import "fmt"

type Node struct {
    value int
    next  *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) append(val int) {
    newNode := &Node{value: val}
    if l.head == nil {
        l.head = newNode
        return
    }
    current := l.head
    for current.next != nil {
        current = current.next
    }
    current.next = newNode
}

func (l *LinkedList) deleteByValue(val int) {
    if l.head == nil {
        return
    }
    if l.head.value == val {
        l.head = l.head.next
        return
    }
    current := l.head
    for current.next != nil && current.next.value != val {
        current = current.next
    }
    if current.next != nil {
        current.next = current.next.next
    }
}

func (l *LinkedList) printList() {
    current := l.head
    for current != nil {
        fmt.Printf("%d -> ", current.value)
        current = current.next
    }
    fmt.Println("null")
}

func main() {
    list := LinkedList{}
    list.append(1)
    list.append(2)
    list.append(3)
    list.append(4)
    list.deleteByValue(1)
    list.deleteByValue(2)
    list.deleteByValue(3)
    list.deleteByValue(4)
    list.printList()
}
Anull
B1 -> 2 -> 3 -> 4 -> null
C4 -> null
D1 -> null
Attempts:
2 left
💡 Hint
Deleting all nodes one by one empties the list.

Practice

(1/5)
1. What is the main purpose of the Delete Node by Value operation in a linked list?
easy
A. To find the position of a node with the given value
B. To remove the first node that contains the given value from the list
C. To add a new node with the given value at the end of the list
D. To reverse the linked list

Solution

  1. Step 1: Understand the operation's goal

    The operation aims to remove a node that matches a specific value from the linked list.
  2. Step 2: Identify the correct description

    Removing the first node with the given value matches To remove the first node that contains the given value from the list, while others describe different operations.
  3. Final Answer:

    To remove the first node that contains the given value from the list -> Option B
  4. Quick Check:

    Delete node by value means remove matching node [OK]
Hint: Delete node by value means remove matching node [OK]
Common Mistakes:
  • Confusing deletion with insertion
  • Thinking it finds position instead of deleting
  • Assuming it reverses the list
2. Which of the following Go code snippets correctly deletes the first node with value 5 from a singly linked list starting at head?
easy
A. if head == nil { return head } if head.val == 5 { return head.next } curr := head for curr.next != nil && curr.next.val != 5 { curr = curr.next } if curr.next != nil { curr.next = curr.next.next } return head
B. if head == nil { return nil } curr := head for curr != nil && curr.val != 5 { curr = curr.next } if curr != nil { curr = curr.next } return head
C. if head.val == 5 { head = nil } curr := head for curr.next != nil { if curr.next.val == 5 { curr.next = nil } curr = curr.next } return head
D. curr := head for curr.next != nil { if curr.val == 5 { curr = curr.next.next } curr = curr.next } return head

Solution

  1. Step 1: Check for empty list and head node value

    if head == nil { return head } if head.val == 5 { return head.next } curr := head for curr.next != nil && curr.next.val != 5 { curr = curr.next } if curr.next != nil { curr.next = curr.next.next } return head correctly returns head.next if head's value is 5, handling deletion at the start.
  2. Step 2: Traverse and delete node with value 5

    if head == nil { return head } if head.val == 5 { return head.next } curr := head for curr.next != nil && curr.next.val != 5 { curr = curr.next } if curr.next != nil { curr.next = curr.next.next } return head uses a loop to find the node before the target and updates links properly; others miss this or incorrectly update pointers.
  3. Final Answer:

    Option A code correctly deletes the first node with value 5 -> Option A
  4. Quick Check:

    Proper pointer update and head check = if head == nil { return head } if head.val == 5 { return head.next } curr := head for curr.next != nil && curr.next.val != 5 { curr = curr.next } if curr.next != nil { curr.next = curr.next.next } return head [OK]
Hint: Check head value first, then update next pointers carefully [OK]
Common Mistakes:
  • Not handling deletion when head node matches
  • Incorrect pointer updates causing list break
  • Skipping traversal or deleting wrong node
3. Given the linked list: 1 -> 3 -> 5 -> 7 -> 5 -> null, what will be the list after deleting the first node with value 5 using the standard delete-by-value function?
medium
A. 1 -> 3 -> 5 -> 7 -> null
B. 1 -> 3 -> 7 -> null
C. 3 -> 5 -> 7 -> 5 -> null
D. 1 -> 3 -> 7 -> 5 -> null

Solution

  1. Step 1: Identify the first node with value 5

    The first 5 appears as the third node in the list (1 -> 3 -> 5).
  2. Step 2: Remove that node and link previous to next

    Deleting this node links 3 directly to 7, resulting in 1 -> 3 -> 7 -> 5 -> null.
  3. Final Answer:

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

    First 5 removed, rest unchanged = 1 -> 3 -> 7 -> 5 -> null [OK]
Hint: Delete only first matching node, keep rest intact [OK]
Common Mistakes:
  • Deleting all nodes with value 5
  • Removing wrong node (second 5)
  • Breaking list links incorrectly
4. The following Go code attempts to delete a node by value from a singly linked list but causes a runtime panic. What is the error?
func deleteNode(head *Node, val int) *Node {
    curr := head
    for curr.next != nil {
        if curr.next.val == val {
            curr.next = curr.next.next
            break
        }
        curr = curr.next
    }
    return head
}
medium
A. It does not handle deletion if the head node contains the value
B. It causes infinite loop due to missing curr update
C. It tries to access curr.next.val without checking if curr.next is nil
D. It deletes all nodes with the value instead of just the first

Solution

  1. Step 1: Analyze head node deletion case

    The code starts from head and checks curr.next, so if head itself has the value, it is not deleted.
  2. Step 2: Confirm no panic cause

    The loop condition curr.next != nil prevents nil pointer dereference, so no panic from accessing curr.next.val.
  3. Final Answer:

    It does not handle deletion if the head node contains the value -> Option A
  4. Quick Check:

    Head node deletion missing = It does not handle deletion if the head node contains the value [OK]
Hint: Always check head node separately before looping [OK]
Common Mistakes:
  • Ignoring head node deletion
  • Assuming panic from nil pointer without checking
  • Not breaking after deletion causing multiple deletes
5. You have a singly linked list with nodes containing values: 2 -> 4 -> 6 -> 4 -> 8 -> null. You want to delete all nodes with value 4 using a single pass delete-by-value function that deletes only the first matching node per call. How many times must you call this function to remove all 4s, and what will the list look like after all deletions?
hard
A. Call twice; final list: 2 -> 4 -> 6 -> 8 -> null
B. Call once; final list: 2 -> 6 -> 4 -> 8 -> null
C. Call twice; final list: 2 -> 6 -> 8 -> null
D. Call once; final list: 2 -> 6 -> 8 -> null

Solution

  1. Step 1: Understand single pass deletion removes only first matching node

    Each call deletes only the first node with value 4 found.
  2. Step 2: Count calls needed and final list

    First call removes the first 4 (second node), list becomes 2 -> 6 -> 4 -> 8 -> null. Second call removes the next 4 (third node), list becomes 2 -> 6 -> 8 -> null.
  3. Final Answer:

    Call twice; final list: 2 -> 6 -> 8 -> null -> Option C
  4. Quick Check:

    Two calls remove both 4s = Call twice; final list: 2 -> 6 -> 8 -> null [OK]
Hint: One call deletes one node; repeat until all removed [OK]
Common Mistakes:
  • Assuming one call removes all matching nodes
  • Miscounting nodes deleted per call
  • Confusing node positions after deletions
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