Bird
Raised Fist0
DSA Goprogramming~3 mins

Why Reverse a Doubly Linked List in DSA Go?

Choose your learning style10 modes available
The Big Idea

What if you could flip a list instantly without moving a single item?

The Scenario

Imagine you have a list of your favorite songs saved in order. Now, you want to play them backward, from last to first. Doing this by hand means rewriting the entire list in reverse, which takes time and can cause mistakes.

The Problem

Manually reversing a list means moving each item one by one to a new list in reverse order. This is slow and easy to mess up, especially if the list is long. You might lose track or accidentally skip songs.

The Solution

Reversing a doubly linked list swaps the direction of the links between nodes. Instead of moving data around, we just change pointers, making the process fast and safe. This way, the list order flips instantly without copying or losing items.

Before vs After
✗ Before
for i := len(list) - 1; i >= 0; i-- {
    reversedList = append(reversedList, list[i])
}
✓ After
func reverseDoublyLinkedList(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
}
What It Enables

This lets you quickly flip the order of items in a list without extra space or copying, making your programs faster and more efficient.

Real Life Example

Music players use this to reverse playlists instantly when you choose the "play backward" option, without creating a new list or waiting.

Key Takeaways

Manual reversal is slow and error-prone.

Reversing pointers in a doubly linked list is fast and safe.

This technique saves time and memory in many applications.

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