Challenge - 5 Problems
Singly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Inserting Nodes at the End
What is the printed state of the singly linked list after inserting nodes with values 10, 20, and 30 at the end?
DSA Go
package main import "fmt" type Node struct { value int next *Node } type LinkedList struct { head *Node } func (l *LinkedList) InsertEnd(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) PrintList() { current := l.head for current != nil { fmt.Printf("%d -> ", current.value) current = current.next } fmt.Println("null") } func main() { list := LinkedList{} list.InsertEnd(10) list.InsertEnd(20) list.InsertEnd(30) list.PrintList() }
Attempts:
2 left
💡 Hint
Think about how nodes are added at the end and how the list is printed from head to tail.
✗ Incorrect
Nodes are inserted at the end, so the order is the same as insertion: 10, then 20, then 30. The print starts from head and moves forward.
❓ Predict Output
intermediateOutput After Deleting the Head Node
What is the printed state of the singly linked list after deleting the head node from the list 5 -> 15 -> 25 -> null?
DSA Go
package main import "fmt" type Node struct { value int next *Node } type LinkedList struct { head *Node } func (l *LinkedList) DeleteHead() { if l.head != nil { l.head = l.head.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{ head: &Node{value: 5, next: &Node{value: 15, next: &Node{value: 25}}}, } list.DeleteHead() list.PrintList() }
Attempts:
2 left
💡 Hint
Deleting the head means the second node becomes the new head.
✗ Incorrect
After deleting the head (5), the new head is 15, so the list is 15 -> 25 -> null.
🔧 Debug
advancedIdentify the Bug in Node Insertion at Beginning
What error will occur when running this code that tries to insert a node at the beginning of the singly linked list?
DSA Go
package main import "fmt" type Node struct { value int next *Node } type LinkedList struct { head *Node } func (l *LinkedList) InsertBeginning(val int) { newNode := &Node{value: val} newNode.next = l.head l.head = newNode } func (l *LinkedList) PrintList() { current := l.head for current != nil { fmt.Printf("%d -> ", current.value) current = current.next } fmt.Println("null") } func main() { var list *LinkedList list.InsertBeginning(100) list.PrintList() }
Attempts:
2 left
💡 Hint
Check how the list variable is initialized before calling methods.
✗ Incorrect
The list variable is nil, so calling InsertBeginning on it causes a runtime nil pointer dereference error.
❓ Predict Output
advancedOutput After Reversing a Singly Linked List
What is the printed state of the singly linked list after reversing the list 1 -> 2 -> 3 -> 4 -> null?
DSA Go
package main import "fmt" type Node struct { value int next *Node } type LinkedList struct { head *Node } func (l *LinkedList) Reverse() { var prev *Node current := l.head for current != nil { nextNode := current.next current.next = prev prev = current current = nextNode } l.head = prev } 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{ head: &Node{value: 1, next: &Node{value: 2, next: &Node{value: 3, next: &Node{value: 4}}}}, } list.Reverse() list.PrintList() }
Attempts:
2 left
💡 Hint
Reversing flips the order of nodes from head to tail.
✗ Incorrect
The reverse function changes the links so the last node becomes the head, reversing the list order.
🧠 Conceptual
expertNumber of Nodes After Multiple Insertions and Deletions
Starting with an empty singly linked list, the following operations are performed in order:
1. Insert 5 nodes at the beginning
2. Insert 3 nodes at the end
3. Delete 4 nodes from the beginning
4. Insert 2 nodes at the beginning
How many nodes remain in the list after these operations?
Attempts:
2 left
💡 Hint
Count nodes after each step carefully.
✗ Incorrect
After step 1: 5 nodes
After step 2: 8 nodes
After step 3: 4 nodes (8 - 4)
After step 4: 6 nodes (4 + 2)
