Bird
Raised Fist0
DSA Goprogramming~20 mins

Reverse a Doubly Linked List in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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
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)
}
Anull
B1 <-> 2 <-> 3 <-> null
C3 <-> 2 <-> 1 <-> null
D3 <-> 1 <-> 2 <-> null
Attempts:
2 left
💡 Hint
Think about swapping the next and prev pointers for each node.
🧠 Conceptual
intermediate
1:30remaining
Understanding pointer changes in reversing a doubly linked list
When reversing a doubly linked list, which pointer changes are made for each node?
AOnly change the next pointer to point to the previous node
BSwap the next and prev pointers of each node
COnly change the prev pointer to point to the next node
DRemove all prev pointers and reverse the next pointers
Attempts:
2 left
💡 Hint
Think about what makes a doubly linked list different from a singly linked list.
🔧 Debug
advanced
2:00remaining
Identify 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
}
AInfinite loop due to pointers not swapping correctly
BNil pointer dereference runtime error
CCorrect reversal with no error
DCompilation error due to missing variable
Attempts:
2 left
💡 Hint
Check how pointers are assigned inside the loop.
❓ Predict Output
advanced
1:30remaining
Output 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
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)
}
A5 <-> 5 <-> null
Bnull
CError: nil pointer dereference
D5 <-> null
Attempts:
2 left
💡 Hint
Reversing a single node list should not change the list.
🚀 Application
expert
1:00remaining
Number 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?
A7
B14
C0
D1
Attempts:
1 left
💡 Hint
Reversing changes the order but not the number of nodes.

Practice

(1/5)
1. What is the main purpose of reversing a doubly linked list?
easy
A. To sort the list in ascending order
B. To delete all nodes from the list
C. To make the last node the first and the first node the last
D. To add a new node at the end

Solution

  1. Step 1: Understand the structure of a doubly linked list

    A doubly linked list has nodes with pointers to both previous and next nodes.
  2. Step 2: Define reversing operation

    Reversing means swapping the previous and next pointers for all nodes, so the last node becomes the first.
  3. Final Answer:

    To make the last node the first and the first node the last -> Option C
  4. Quick Check:

    Reverse = swap previous and next pointers [OK]
Hint: Reversing swaps next and previous pointers [OK]
Common Mistakes:
  • Confusing reversing with sorting
  • Thinking reversing deletes nodes
  • Assuming reversing adds nodes
2. Which of the following Go code snippets correctly swaps the next and prev pointers of a node curr in a doubly linked list?
easy
A. curr.next, curr.prev = curr.prev, curr.next
B. curr.next = curr.prev; curr.prev = curr.next
C. curr.prev = curr.next; curr.next = curr.prev
D. curr.next = curr.next; curr.prev = curr.prev

Solution

  1. Step 1: Understand multiple assignment in Go

    Go allows swapping two variables in one line using multiple assignment.
  2. Step 2: Identify correct swap syntax

    The correct way to swap is curr.next, curr.prev = curr.prev, curr.next which exchanges the values.
  3. Final Answer:

    curr.next, curr.prev = curr.prev, curr.next -> Option A
  4. Quick Check:

    Swap pointers with multiple assignment [OK]
Hint: Use Go's multiple assignment to swap pointers [OK]
Common Mistakes:
  • Assigning one pointer twice without swapping
  • Using separate lines causing overwrite
  • Not swapping pointers at all
3. Given the following Go code to reverse a doubly linked list, what will be the printed output after reversal?
type Node struct {
    data int
    prev *Node
    next *Node
}

func reverse(head *Node) *Node {
    var temp *Node
    curr := head
    for curr != nil {
        temp = curr.prev
        curr.prev = curr.next
        curr.next = temp
        curr = curr.prev
    }
    if temp != nil {
        head = temp.prev
    }
    return head
}

// List before reversal: 1 <-> 2 <-> 3 <-> nil
// After reversal, print from new head
func printList(head *Node) {
    curr := head
    for curr != nil {
        fmt.Print(curr.data, " <-> ")
        curr = curr.next
    }
    fmt.Println("nil")
}
medium
A. 1 <-> 2 <-> 3 <-> nil
B. 3 <-> 2 <-> 1 <-> nil
C. nil
D. 1 <-> nil

Solution

  1. Step 1: Trace pointer swaps in the loop

    Each node swaps its prev and next pointers, moving curr to original next node via curr.prev after swap.
  2. Step 2: Update head to last processed node

    After loop, head is set to temp.prev which points to the new first node (original last node).
  3. Final Answer:

    3 <-> 2 <-> 1 <-> nil -> Option B
  4. Quick Check:

    Reversed list prints nodes in reverse order [OK]
Hint: Reversed list prints nodes backward [OK]
Common Mistakes:
  • Not updating head after reversal
  • Confusing prev and next pointers
  • Printing original list without reversal
4. Identify the bug in this Go function that attempts to reverse a doubly linked list:
func reverse(head *Node) *Node {
    curr := head
    for curr != nil {
        curr.prev, curr.next = curr.next, curr.prev
        curr = curr.next
    }
    return head
}
medium
A. The loop moves curr to curr.next instead of curr.prev after swapping
B. The pointers are not swapped correctly
C. The function does not return the new head
D. The function deletes nodes accidentally

Solution

  1. Step 1: Analyze pointer swapping and traversal

    The pointers are swapped correctly, but after swapping, curr.next points to the previous node, so moving curr to curr.next moves backward.
  2. Step 2: Identify correct traversal direction

    After swapping, curr should move to curr.prev (original next) to continue forward traversal.
  3. Final Answer:

    The loop moves curr to curr.next instead of curr.prev after swapping -> Option A
  4. Quick Check:

    Traverse using curr.prev after swap [OK]
Hint: After swap, move curr to curr.prev, not curr.next [OK]
Common Mistakes:
  • Moving curr in wrong direction after swap
  • Not updating head to new first node
  • Swapping pointers incorrectly
5. You have a doubly linked list with nodes containing integers. After reversing it using the standard pointer swap method, you want to find the sum of all nodes at even positions (2nd, 4th, etc.) in the reversed list. Which approach correctly combines reversal and summation?
hard
A. Sum all nodes without reversal
B. Sum nodes at even positions before reversal, then reverse the list
C. Reverse the list and sum nodes at odd positions instead
D. First reverse the list, then traverse from new head summing nodes at even positions

Solution

  1. Step 1: Reverse the doubly linked list

    Use pointer swapping to reverse the list so the order is changed.
  2. Step 2: Traverse from new head and sum nodes at even positions

    After reversal, positions change, so summing even positions requires traversal from new head counting nodes.
  3. Final Answer:

    First reverse the list, then traverse from new head summing nodes at even positions -> Option D
  4. Quick Check:

    Reverse first, then sum even positions [OK]
Hint: Reverse first, then sum nodes by position [OK]
Common Mistakes:
  • Summing before reversal causing wrong positions
  • Summing odd positions instead of even
  • Ignoring reversal effect on node order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard