Challenge - 5 Problems
Linked List Push Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after pushing nodes in this linked list?
Consider a singly linked list where nodes are pushed to the front. What is the printed linked list after these push operations?
DSA Go
package main import "fmt" type Node struct { data int next *Node } type LinkedList struct { head *Node } func (l *LinkedList) push(newNode *Node) { newNode.next = l.head l.head = newNode } func (l *LinkedList) printList() { current := l.head for current != nil { fmt.Printf("%d -> ", current.data) current = current.next } fmt.Print("null\n") } func main() { list := LinkedList{} list.push(&Node{data: 10}) list.push(&Node{data: 20}) list.push(&Node{data: 30}) list.printList() }
Attempts:
2 left
💡 Hint
Remember, pushing a node adds it to the front of the list.
✗ Incorrect
Each push adds the new node at the head, so the last pushed node becomes the first in the list.
❓ Predict Output
intermediateWhat is the output after pushing nodes with duplicate values?
Given the linked list push method, what is the printed list after pushing nodes with values 5, 5, and 10 in that order?
DSA Go
package main import "fmt" type Node struct { data int next *Node } type LinkedList struct { head *Node } func (l *LinkedList) push(newNode *Node) { newNode.next = l.head l.head = newNode } func (l *LinkedList) printList() { current := l.head for current != nil { fmt.Printf("%d -> ", current.data) current = current.next } fmt.Print("null\n") } func main() { list := LinkedList{} list.push(&Node{data: 5}) list.push(&Node{data: 5}) list.push(&Node{data: 10}) list.printList() }
Attempts:
2 left
💡 Hint
Pushing adds nodes to the front regardless of value.
✗ Incorrect
The last pushed node with value 10 becomes the head, followed by the two 5s.
🔧 Debug
advancedWhy does this push method cause a runtime error?
This push method sometimes causes a runtime panic. What is the cause?
DSA Go
func (l *LinkedList) push(newNode *Node) {
newNode.next = l.head.next
l.head = newNode
}Attempts:
2 left
💡 Hint
Check what happens when the list is empty and you access l.head.next.
✗ Incorrect
When the list is empty, l.head is nil, so accessing l.head.next causes a runtime panic.
🧠 Conceptual
advancedWhat is the time complexity of pushing a node to the front of a singly linked list?
Consider the push operation that adds a node at the front of a singly linked list. What is its time complexity?
Attempts:
2 left
💡 Hint
Pushing only changes the head pointer and the new node's next pointer.
✗ Incorrect
Pushing a node to the front requires only a few pointer changes, so it runs in constant time.
🚀 Application
expertAfter pushing nodes, what is the value of the third node in the list?
Given the push operations below, what is the data value of the third node from the head?
DSA Go
package main type Node struct { data int next *Node } type LinkedList struct { head *Node } func (l *LinkedList) push(newNode *Node) { newNode.next = l.head l.head = newNode } func main() { list := LinkedList{} list.push(&Node{data: 1}) list.push(&Node{data: 2}) list.push(&Node{data: 3}) list.push(&Node{data: 4}) // What is the data of the third node? }
Attempts:
2 left
💡 Hint
Remember the order nodes are pushed and how the list looks after each push.
✗ Incorrect
Nodes are pushed to the front, so the list is 4 -> 3 -> 2 -> 1 -> null. The third node is 2.
