Challenge - 5 Problems
Doubly Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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() }
Attempts:
2 left
💡 Hint
Deleting a node by value removes the first node with that value and links its neighbors.
✗ Incorrect
The node with value 3 is removed. The previous node (2) points to the next node (4), and the next node (4) points back to the previous node (2). The list becomes 1 -> 2 -> 4 -> 5 -> null.
❓ Predict Output
intermediateOutput 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() }
Attempts:
2 left
💡 Hint
Deleting the head node updates the head pointer to the next node.
✗ Incorrect
The head node with value 1 is removed. The new head is the node with value 2. The list becomes 2 -> 3 -> null.
❓ Predict Output
advancedOutput 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() }
Attempts:
2 left
💡 Hint
Deleting the tail node updates the tail pointer to the previous node.
✗ Incorrect
The tail node with value 5 is removed. The new tail is the node with value 4. The list becomes 1 -> 2 -> 3 -> 4 -> null.
❓ Predict Output
advancedOutput 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() }
Attempts:
2 left
💡 Hint
If the value is not found, the list remains unchanged.
✗ Incorrect
Since the value 10 does not exist in the list, no deletion happens. The list remains 1 -> 2 -> 3 -> null.
🧠 Conceptual
expertEffect 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?
Attempts:
2 left
💡 Hint
Deleting all nodes one by one removes all links and updates head and tail accordingly.
✗ Incorrect
Each deleteByValue call removes one node and updates head and tail pointers. After all nodes are deleted, head and tail become nil, so the list is empty.
