Challenge - 5 Problems
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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() }
Attempts:
2 left
💡 Hint
Think about how the deleteByValue function skips the node with the matching value.
✗ Incorrect
The function deletes the node with value 3 by linking the node with value 2 directly to the node with value 4, so the printed list skips 3.
❓ Predict Output
intermediateResult 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() }
Attempts:
2 left
💡 Hint
Deleting the head node means the head pointer moves to the next node.
✗ Incorrect
The head node with value 1 is removed, so the new head is the node with value 2.
❓ Predict Output
advancedOutput 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() }
Attempts:
2 left
💡 Hint
If the value is not found, the list remains unchanged.
✗ Incorrect
Since value 5 is not in the list, no node is deleted and the list stays the same.
❓ Predict Output
advancedOutput 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() }
Attempts:
2 left
💡 Hint
Deleting the last node means the second last node's next pointer becomes nil.
✗ Incorrect
The node with value 4 is removed, so the list ends at node with value 3.
❓ Predict Output
expertOutput 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() }
Attempts:
2 left
💡 Hint
Deleting all nodes one by one empties the list.
✗ Incorrect
After deleting all nodes, the list is empty and prints null.
