Challenge - 5 Problems
Delete Node at End Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting the last node from a singly linked list
What is the printed state of the linked list after deleting the last node?
DSA Go
package main import "fmt" type Node struct { data int next *Node } type LinkedList struct { head *Node } func (l *LinkedList) append(data int) { newNode := &Node{data: data} if l.head == nil { l.head = newNode return } current := l.head for current.next != nil { current = current.next } current.next = newNode } func (l *LinkedList) deleteEnd() { if l.head == nil { return } if l.head.next == nil { l.head = nil return } current := l.head for current.next.next != nil { current = current.next } current.next = nil } func (l *LinkedList) printList() { current := l.head for current != nil { fmt.Printf("%d -> ", current.data) current = current.next } fmt.Println("null") } func main() { list := LinkedList{} list.append(10) list.append(20) list.append(30) list.deleteEnd() list.printList() }
Attempts:
2 left
💡 Hint
Think about what happens to the last node when you delete it.
✗ Incorrect
The code deletes the last node (which has value 30). After deletion, the list contains nodes with values 10 and 20 only.
❓ Predict Output
intermediateOutput after deleting the last node from a single-node linked list
What is the printed state of the linked list after deleting the last node when the list has only one node?
DSA Go
package main import "fmt" type Node struct { data int next *Node } type LinkedList struct { head *Node } func (l *LinkedList) append(data int) { newNode := &Node{data: data} if l.head == nil { l.head = newNode return } current := l.head for current.next != nil { current = current.next } current.next = newNode } func (l *LinkedList) deleteEnd() { if l.head == nil { return } if l.head.next == nil { l.head = nil return } current := l.head for current.next.next != nil { current = current.next } current.next = nil } func (l *LinkedList) printList() { current := l.head for current != nil { fmt.Printf("%d -> ", current.data) current = current.next } fmt.Println("null") } func main() { list := LinkedList{} list.append(100) list.deleteEnd() list.printList() }
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
✗ Incorrect
After deleting the only node, the list becomes empty, so printing shows 'null'.
🔧 Debug
advancedIdentify the error in deleteEnd method for empty list
What error will occur if deleteEnd is called on an empty linked list with the following code?
DSA Go
func (l *LinkedList) deleteEnd() {
current := l.head
for current.next.next != nil {
current = current.next
}
current.next = nil
}Attempts:
2 left
💡 Hint
Consider what happens when l.head is nil.
✗ Incorrect
If the list is empty (l.head is nil), accessing current.next causes a nil pointer dereference error.
❓ Predict Output
advancedOutput after multiple deletions at end
What is the printed state of the linked list after deleting the last node twice in a list with nodes 5 -> 15 -> 25 -> 35?
DSA Go
package main import "fmt" type Node struct { data int next *Node } type LinkedList struct { head *Node } func (l *LinkedList) append(data int) { newNode := &Node{data: data} if l.head == nil { l.head = newNode return } current := l.head for current.next != nil { current = current.next } current.next = newNode } func (l *LinkedList) deleteEnd() { if l.head == nil { return } if l.head.next == nil { l.head = nil return } current := l.head for current.next.next != nil { current = current.next } current.next = nil } func (l *LinkedList) printList() { current := l.head for current != nil { fmt.Printf("%d -> ", current.data) current = current.next } fmt.Println("null") } func main() { list := LinkedList{} list.append(5) list.append(15) list.append(25) list.append(35) list.deleteEnd() list.deleteEnd() list.printList() }
Attempts:
2 left
💡 Hint
Each deleteEnd removes the last node.
✗ Incorrect
After first deletion, list is 5 -> 15 -> 25. After second deletion, list is 5 -> 15.
🧠 Conceptual
expertWhy is it necessary to traverse to the second last node to delete the last node in a singly linked list?
Choose the best explanation for why deleting the last node in a singly linked list requires traversal to the second last node.
Attempts:
2 left
💡 Hint
Think about how nodes are connected in a singly linked list.
✗ Incorrect
In a singly linked list, each node points only to the next node. To remove the last node, we must update the next pointer of the node before it to nil. Since nodes don't have backward pointers, we must traverse to the second last node.
