Bird
Raised Fist0
DSA Goprogramming~20 mins

Delete by Value in Doubly Linked List in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly 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 middle node by value
What is the printed state of the doubly linked list after deleting the node with value 3?
DSA Go
package main

import "fmt"

type Node struct {
    value int
    prev  *Node
    next  *Node
}

type DoublyLinkedList struct {
    head *Node
    tail *Node
}

func (dll *DoublyLinkedList) append(value int) {
    newNode := &Node{value: value}
    if dll.head == nil {
        dll.head = newNode
        dll.tail = newNode
        return
    }
    dll.tail.next = newNode
    newNode.prev = dll.tail
    dll.tail = newNode
}

func (dll *DoublyLinkedList) deleteByValue(value int) {
    current := dll.head
    for current != nil {
        if current.value == value {
            if current.prev != nil {
                current.prev.next = current.next
            } else {
                dll.head = current.next
            }
            if current.next != nil {
                current.next.prev = current.prev
            } else {
                dll.tail = current.prev
            }
            return
        }
        current = current.next
    }
}

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

func main() {
    dll := DoublyLinkedList{}
    dll.append(1)
    dll.append(2)
    dll.append(3)
    dll.append(4)
    dll.append(5)
    dll.deleteByValue(3)
    dll.printList()
}
A1 -> 2 -> 4 -> null
B1 -> 2 -> 3 -> 4 -> 5 -> null
C2 -> 3 -> 4 -> 5 -> null
D1 -> 2 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Deleting a node by value removes the first node with that value and links its neighbors.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the head node by value
What is the printed state of the doubly linked list after deleting the node with value 1 (head node)?
DSA Go
package main

import "fmt"

type Node struct {
    value int
    prev  *Node
    next  *Node
}

type DoublyLinkedList struct {
    head *Node
    tail *Node
}

func (dll *DoublyLinkedList) append(value int) {
    newNode := &Node{value: value}
    if dll.head == nil {
        dll.head = newNode
        dll.tail = newNode
        return
    }
    dll.tail.next = newNode
    newNode.prev = dll.tail
    dll.tail = newNode
}

func (dll *DoublyLinkedList) deleteByValue(value int) {
    current := dll.head
    for current != nil {
        if current.value == value {
            if current.prev != nil {
                current.prev.next = current.next
            } else {
                dll.head = current.next
            }
            if current.next != nil {
                current.next.prev = current.prev
            } else {
                dll.tail = current.prev
            }
            return
        }
        current = current.next
    }
}

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

func main() {
    dll := DoublyLinkedList{}
    dll.append(1)
    dll.append(2)
    dll.append(3)
    dll.deleteByValue(1)
    dll.printList()
}
A3 -> null
B2 -> 3 -> null
C1 -> 2 -> 3 -> null
D2 -> null
Attempts:
2 left
💡 Hint
Deleting the head node updates the head pointer to the next node.
❓ Predict Output
advanced
2:00remaining
Output after deleting the tail node by value
What is the printed state of the doubly linked list after deleting the node with value 5 (tail node)?
DSA Go
package main

import "fmt"

type Node struct {
    value int
    prev  *Node
    next  *Node
}

type DoublyLinkedList struct {
    head *Node
    tail *Node
}

func (dll *DoublyLinkedList) append(value int) {
    newNode := &Node{value: value}
    if dll.head == nil {
        dll.head = newNode
        dll.tail = newNode
        return
    }
    dll.tail.next = newNode
    newNode.prev = dll.tail
    dll.tail = newNode
}

func (dll *DoublyLinkedList) deleteByValue(value int) {
    current := dll.head
    for current != nil {
        if current.value == value {
            if current.prev != nil {
                current.prev.next = current.next
            } else {
                dll.head = current.next
            }
            if current.next != nil {
                current.next.prev = current.prev
            } else {
                dll.tail = current.prev
            }
            return
        }
        current = current.next
    }
}

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

func main() {
    dll := DoublyLinkedList{}
    dll.append(1)
    dll.append(2)
    dll.append(3)
    dll.append(4)
    dll.append(5)
    dll.deleteByValue(5)
    dll.printList()
}
A1 -> 2 -> 3 -> null
B1 -> 2 -> 3 -> 4 -> 5 -> null
C1 -> 2 -> 3 -> 4 -> null
D2 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Deleting the tail node updates the tail pointer to the previous node.
❓ Predict Output
advanced
2:00remaining
Output after deleting a non-existing value
What is the printed state of the doubly linked list after trying to delete the node with value 10 (which does not exist)?
DSA Go
package main

import "fmt"

type Node struct {
    value int
    prev  *Node
    next  *Node
}

type DoublyLinkedList struct {
    head *Node
    tail *Node
}

func (dll *DoublyLinkedList) append(value int) {
    newNode := &Node{value: value}
    if dll.head == nil {
        dll.head = newNode
        dll.tail = newNode
        return
    }
    dll.tail.next = newNode
    newNode.prev = dll.tail
    dll.tail = newNode
}

func (dll *DoublyLinkedList) deleteByValue(value int) {
    current := dll.head
    for current != nil {
        if current.value == value {
            if current.prev != nil {
                current.prev.next = current.next
            } else {
                dll.head = current.next
            }
            if current.next != nil {
                current.next.prev = current.prev
            } else {
                dll.tail = current.prev
            }
            return
        }
        current = current.next
    }
}

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

func main() {
    dll := DoublyLinkedList{}
    dll.append(1)
    dll.append(2)
    dll.append(3)
    dll.deleteByValue(10)
    dll.printList()
}
A1 -> 2 -> 3 -> null
Bnull
C2 -> 3 -> null
D1 -> 3 -> null
Attempts:
2 left
💡 Hint
If the value is not found, the list remains unchanged.
🧠 Conceptual
expert
2:00remaining
Effect of deleting all nodes by repeated deleteByValue calls
If you repeatedly call deleteByValue on a doubly linked list to remove all nodes one by one by their values, what will be the state of the list at the end?
AThe list will be empty with head and tail pointers set to nil.
BThe list will contain the last deleted node only.
CThe list will be empty but head and tail pointers will still point to the last node.
DThe list will be unchanged because deleteByValue only removes one node per call.
Attempts:
2 left
💡 Hint
Deleting all nodes one by one removes all links and updates head and tail accordingly.

Practice

(1/5)
1. What happens when you delete a node by value in a doubly linked list?
easy
A. The node with the matching value is removed and links are updated
B. All nodes are deleted regardless of value
C. Only the first node is deleted no matter the value
D. The list is reversed instead of deleting

Solution

  1. Step 1: Locate the node with the given value

    Traverse the doubly linked list from head to tail to find the node whose data matches the value to delete.
  2. Step 2: Update the links to remove the node

    Adjust the previous node's next pointer and the next node's previous pointer to bypass the node to be deleted, effectively removing it from the list.
  3. Final Answer:

    The node with the matching value is removed and links are updated -> Option A
  4. Quick Check:

    Delete by value means remove matching node and fix links [OK]
Hint: Find node by value, then fix previous and next links [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Not updating next node's previous pointer
  • Deleting wrong node when multiple have same value
2. Which of the following Go code snippets correctly deletes a node by value from a doubly linked list?
easy
A. if current.data == value { current.next.next = current.prev.prev }
B. if current.data == value { current.next = current.prev; current.prev = current.next }
C. if current.data == value { current = nil }
D. if current.data == value { current.prev.next = current.next; current.next.prev = current.prev }

Solution

  1. Step 1: Check node data against value

    The condition current.data == value correctly identifies the node to delete.
  2. Step 2: Update links correctly

    Setting current.prev.next = current.next and current.next.prev = current.prev properly removes the node by linking its neighbors.
  3. Final Answer:

    if current.data == value { current.prev.next = current.next; current.next.prev = current.prev } -> Option D
  4. Quick Check:

    Correct link updates remove node [OK]
Hint: Update prev.next and next.prev to remove node [OK]
Common Mistakes:
  • Swapping prev and next pointers incorrectly
  • Setting current to nil without fixing links
  • Accessing next or prev without nil checks causing panic
3. Given the doubly linked list: 1 <-> 2 <-> 3 <-> 4 <-> 5, what is the list after deleting the node with value 3?
medium
A. 1 <-> 2 <-> 4 <-> 3 <-> 5
B. 1 <-> 2 <-> 4 <-> 5
C. 2 <-> 3 <-> 4 <-> 5
D. 1 <-> 2 <-> 3 <-> 5

Solution

  1. Step 1: Identify node with value 3

    Traverse the list to find the node containing 3, which is between nodes 2 and 4.
  2. Step 2: Remove node 3 and update links

    Set node 2's next pointer to node 4 and node 4's previous pointer to node 2, skipping node 3.
  3. Final Answer:

    1 <-> 2 <-> 4 <-> 5 -> Option B
  4. Quick Check:

    Node 3 removed, neighbors linked [OK]
Hint: Remove node and link neighbors directly [OK]
Common Mistakes:
  • Leaving node 3 linked causing broken list
  • Deleting wrong node by value
  • Not updating both prev and next pointers
4. Consider this Go code snippet to delete a node by value in a doubly linked list:
func deleteByValue(head *Node, value int) *Node {
    current := head
    for current != nil {
        if current.data == value {
            if current.prev != nil {
                current.prev.next = current.next
            } else {
                head = current.next
            }
            if current.next != nil {
                current.next.prev = current.prev
            }
            break
        }
        current = current.next
    }
    return head
}
What is the bug in this code?
medium
A. It does not handle deleting the last node correctly
B. It does not update head when deleting the first node
C. It breaks after deleting only the first matching node
D. It does not check for nil pointers before accessing prev or next

Solution

  1. Step 1: Analyze loop and break behavior

    The loop breaks immediately after deleting the first node with matching value, so only one node is deleted even if multiple nodes have the same value.
  2. Step 2: Understand implications for multiple matches

    If the list has multiple nodes with the same value, only the first is removed, which may not be the intended behavior if all matches should be deleted.
  3. Final Answer:

    It breaks after deleting only the first matching node -> Option C
  4. Quick Check:

    Break stops after first delete [OK]
Hint: Check if loop breaks too early when deleting multiple nodes [OK]
Common Mistakes:
  • Ignoring multiple nodes with same value
  • Not updating head when first node deleted
  • Not checking nil before pointer access
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30 <-> 20 <-> 40. You want to delete all nodes with value 20. Which approach correctly deletes all such nodes?
hard
A. Traverse the list and delete nodes with value 20, continuing without breaking the loop
B. Delete the first node with value 20 and then stop traversal
C. Delete nodes with value 20 only if they are at the head or tail
D. Replace nodes with value 20 with a new node containing 0

Solution

  1. Step 1: Traverse entire list without breaking

    To delete all nodes with value 20, you must check every node and not stop after the first deletion.
  2. Step 2: Delete matching nodes and update links carefully

    For each node with value 20, update the previous and next pointers to remove it, then continue traversal to find others.
  3. Final Answer:

    Traverse the list and delete nodes with value 20, continuing without breaking the loop -> Option A
  4. Quick Check:

    Delete all matches by full traversal [OK]
Hint: Do not break loop; delete all matching nodes during traversal [OK]
Common Mistakes:
  • Breaking loop after first deletion
  • Not updating links properly causing broken list
  • Only deleting head or tail nodes
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