Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of reversing a doubly linked list with 3 nodes
What is the printed state of the doubly linked list after reversing it?
The list initially is: 1 <-> 2 <-> 3 <-> null
The list initially is: 1 <-> 2 <-> 3 <-> null
DSA Go
package main import "fmt" type Node struct { data int prev *Node next *Node } func reverse(head *Node) *Node { var temp *Node current := head for current != nil { temp = current.prev current.prev = current.next current.next = temp current = current.prev } if temp != nil { head = temp.prev } return head } func printList(head *Node) { current := head for current != nil { fmt.Print(current.data) if current.next != nil { fmt.Print(" <-> ") } current = current.next } fmt.Println(" <-> null") } func main() { n1 := &Node{data: 1} n2 := &Node{data: 2} n3 := &Node{data: 3} n1.next = n2 n2.prev = n1 n2.next = n3 n3.prev = n2 head := n1 head = reverse(head) printList(head) }
Attempts:
2 left
💡 Hint
Think about swapping the next and prev pointers for each node.
✗ Incorrect
Reversing a doubly linked list means swapping the next and prev pointers for every node. After reversal, the last node becomes the head, so the list prints as 3 <-> 2 <-> 1 <-> null.
🧠 Conceptual
intermediateUnderstanding pointer changes in reversing a doubly linked list
When reversing a doubly linked list, which pointer changes are made for each node?
Attempts:
2 left
💡 Hint
Think about what makes a doubly linked list different from a singly linked list.
✗ Incorrect
In a doubly linked list, each node has two pointers: next and prev. Reversing requires swapping these pointers so the direction of traversal reverses.
🔧 Debug
advancedIdentify the error in this reverse function for doubly linked list
What error will this code cause when reversing a doubly linked list?
DSA Go
func reverse(head *Node) *Node {
current := head
for current != nil {
current.prev = current.next
current.next = current.prev
current = current.prev
}
return head
}Attempts:
2 left
💡 Hint
Check how pointers are assigned inside the loop.
✗ Incorrect
The code assigns current.prev = current.next and then current.next = current.prev, which means current.next is assigned the new current.prev, causing both pointers to be the same and the loop never progresses properly, leading to an infinite loop.
❓ Predict Output
advancedOutput after reversing a doubly linked list with one node
What is the printed state of the doubly linked list after reversing it?
The list initially is: 5 <-> null
The list initially is: 5 <-> null
DSA Go
package main import "fmt" type Node struct { data int prev *Node next *Node } func reverse(head *Node) *Node { var temp *Node current := head for current != nil { temp = current.prev current.prev = current.next current.next = temp current = current.prev } if temp != nil { head = temp.prev } return head } func printList(head *Node) { current := head for current != nil { fmt.Print(current.data) if current.next != nil { fmt.Print(" <-> ") } current = current.next } fmt.Println(" <-> null") } func main() { n1 := &Node{data: 5} head := n1 head = reverse(head) printList(head) }
Attempts:
2 left
💡 Hint
Reversing a single node list should not change the list.
✗ Incorrect
A single node's prev and next pointers are nil, so swapping them does not change the list. The output remains 5 <-> null.
🚀 Application
expertNumber of nodes after reversing a doubly linked list
Given a doubly linked list with 7 nodes, how many nodes will the list have after reversing it?
Attempts:
1 left
💡 Hint
Reversing changes the order but not the number of nodes.
✗ Incorrect
Reversing a doubly linked list only changes the direction of pointers; it does not add or remove nodes. The count remains the same.
