Challenge - 5 Problems
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting node at position 2
What is the printed linked list after deleting the node at position 2 (0-based index)?
DSA Go
package main import "fmt" type Node struct { data int next *Node } func deleteAtPosition(head *Node, pos int) *Node { if head == nil { return nil } if pos == 0 { return head.next } current := head for i := 0; current != nil && i < pos-1; i++ { current = current.next } if current == nil || current.next == nil { return head } current.next = current.next.next return head } func printList(head *Node) { for head != nil { fmt.Printf("%d -> ", head.data) head = head.next } fmt.Println("null") } func main() { head := &Node{data: 1} head.next = &Node{data: 2} head.next.next = &Node{data: 3} head.next.next.next = &Node{data: 4} head = deleteAtPosition(head, 2) printList(head) }
Attempts:
2 left
💡 Hint
Remember that position 2 means the third node in the list.
✗ Incorrect
Deleting the node at position 2 removes the node with data 3. The list becomes 1 -> 2 -> 4 -> null.
❓ Predict Output
intermediateOutput after deleting node at position 0
What is the printed linked list after deleting the node at position 0 (head node)?
DSA Go
package main import "fmt" type Node struct { data int next *Node } func deleteAtPosition(head *Node, pos int) *Node { if head == nil { return nil } if pos == 0 { return head.next } current := head for i := 0; current != nil && i < pos-1; i++ { current = current.next } if current == nil || current.next == nil { return head } current.next = current.next.next return head } func printList(head *Node) { for head != nil { fmt.Printf("%d -> ", head.data) head = head.next } fmt.Println("null") } func main() { head := &Node{data: 10} head.next = &Node{data: 20} head.next.next = &Node{data: 30} head = deleteAtPosition(head, 0) printList(head) }
Attempts:
2 left
💡 Hint
Deleting position 0 removes the head node.
✗ Incorrect
Deleting the head node (position 0) removes the node with data 10. The list becomes 20 -> 30 -> null.
🔧 Debug
advancedIdentify the error in deletion at position function
What error will this code produce when trying to delete a node at position 3 in a list of length 3?
DSA Go
package main import "fmt" type Node struct { data int next *Node } func deleteAtPosition(head *Node, pos int) *Node { if head == nil { return nil } if pos == 0 { return head.next } current := head for i := 0; i < pos-1; i++ { current = current.next } current.next = current.next.next return head } func main() { head := &Node{data: 5} head.next = &Node{data: 10} head.next.next = &Node{data: 15} head = deleteAtPosition(head, 3) fmt.Println("Deletion done") }
Attempts:
2 left
💡 Hint
Check if the code verifies the current or current.next is nil before accessing.
✗ Incorrect
The code does not check if current or current.next is nil before accessing current.next.next, causing a nil pointer dereference at position 3 in a list of length 3.
❓ Predict Output
advancedOutput after deleting node at invalid position
What is the printed linked list after trying to delete a node at position 5 in a list of length 4?
DSA Go
package main import "fmt" type Node struct { data int next *Node } func deleteAtPosition(head *Node, pos int) *Node { if head == nil { return nil } if pos == 0 { return head.next } current := head for i := 0; current != nil && i < pos-1; i++ { current = current.next } if current == nil || current.next == nil { return head } current.next = current.next.next return head } func printList(head *Node) { for head != nil { fmt.Printf("%d -> ", head.data) head = head.next } fmt.Println("null") } func main() { head := &Node{data: 7} head.next = &Node{data: 14} head.next.next = &Node{data: 21} head.next.next.next = &Node{data: 28} head = deleteAtPosition(head, 5) printList(head) }
Attempts:
2 left
💡 Hint
Deleting at a position outside the list length should not change the list.
✗ Incorrect
Position 5 is invalid for a list of length 4, so the list remains unchanged: 7 -> 14 -> 21 -> 28 -> null.
🧠 Conceptual
expertWhy is it important to check for nil before deletion?
In the deleteAtPosition function, why must we check if current or current.next is nil before deleting a node?
Attempts:
2 left
💡 Hint
Think about what happens if you try to access a property of a nil pointer.
✗ Incorrect
Accessing current.next or current.next.next when current or current.next is nil causes a runtime error (nil pointer dereference). Checking prevents this error.
