Bird
Raised Fist0
DSA Cprogramming~20 mins

Reverse a Doubly Linked List in DSA C - 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
What is the printed state of the doubly linked list after reversing it?
DSA C
typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

Node* reverse(Node* head) {
    Node* temp = NULL;
    Node* current = head;
    while (current != NULL) {
        temp = current->prev;
        current->prev = current->next;
        current->next = temp;
        current = current->prev;
    }
    if (temp != NULL) {
        head = temp->prev;
    }
    return head;
}

void printList(Node* node) {
    while (node != NULL) {
        printf("%d", node->data);
        if (node->next != NULL) printf(" <-> ");
        node = node->next;
    }
    printf("\n");
}

int main() {
    Node* head = malloc(sizeof(Node));
    Node* second = malloc(sizeof(Node));
    Node* third = malloc(sizeof(Node));

    head->data = 1; head->prev = NULL; head->next = second;
    second->data = 2; second->prev = head; second->next = third;
    third->data = 3; third->prev = second; third->next = NULL;

    head = reverse(head);
    printList(head);
    return 0;
}
A3 <-> 2 <-> 1
B1 <-> 2 <-> 3
C3 -> 2 -> 1
D1 -> 2 -> 3
Attempts:
2 left
💡 Hint
Think about how the prev and next pointers are swapped in each node.
🧠 Conceptual
intermediate
1:30remaining
Understanding pointer swaps in reversing a doubly linked list
In the reversal of a doubly linked list, what is the main reason for swapping the prev and next pointers of each node?
ATo sort the list in ascending order
BTo free the memory of nodes in reverse order
CTo reverse the direction of the links so traversal order is reversed
DTo duplicate the list nodes
Attempts:
2 left
💡 Hint
Think about how links define the order of nodes.
🔧 Debug
advanced
2:00remaining
Identify the error in this reverse function for doubly linked list
What error will this reverse function cause when reversing a doubly linked list?
DSA C
Node* reverse(Node* head) {
    Node* temp = NULL;
    Node* current = head;
    while (current != NULL) {
        temp = current->next;
        current->next = current->prev;
        current->prev = temp;
        current = current->next;
    }
    if (temp != NULL) {
        head = temp->prev;
    }
    return head;
}
AIt will cause an infinite loop
BIt will reverse the list correctly
CIt will cause a segmentation fault
DIt will leave the list unchanged
Attempts:
2 left
💡 Hint
Check how current pointer moves after swapping.
❓ Predict Output
advanced
2:00remaining
Output after reversing a doubly linked list with 4 nodes
Given a doubly linked list with nodes 10 <-> 20 <-> 30 <-> 40, what is the printed output after applying the reverse function below?
DSA C
Node* reverse(Node* head) {
    Node* temp = NULL;
    Node* current = head;
    while (current != NULL) {
        temp = current->prev;
        current->prev = current->next;
        current->next = temp;
        current = current->prev;
    }
    if (temp != NULL) {
        head = temp->prev;
    }
    return head;
}

// After reversal, printList prints nodes connected by <->
A10 -> 20 -> 30 -> 40
B40 <-> 30 <-> 20 <-> 10
C40 -> 30 -> 20 -> 10
D10 <-> 20 <-> 30 <-> 40
Attempts:
2 left
💡 Hint
The reverse function swaps prev and next pointers for each node.
🧠 Conceptual
expert
1:30remaining
Why is updating the head pointer after reversal necessary?
After reversing a doubly linked list by swapping prev and next pointers, why must we update the head pointer to the last processed node?
ABecause the list nodes are deleted and recreated
BBecause the head pointer always points to NULL after reversal
CBecause the head pointer must point to the middle node after reversal
DBecause the original head becomes the tail and the new head is the last node processed
Attempts:
2 left
💡 Hint
Think about the position of the head before and after reversal.

Practice

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

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 direction so the last node becomes the first and vice versa.
  3. Final Answer:

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

    Reverse = last becomes first [OK]
Hint: Reversing flips start and end nodes [OK]
Common Mistakes:
  • Thinking reversing deletes nodes
  • Confusing reversing with sorting
  • Assuming it changes list type
2. Which of the following is the correct way to swap the next and prev pointers of a node in C?
easy
A. temp = node->prev; node->prev = node->next; node->next = node->next;
B. node->next = node->prev; node->prev = node->next;
C. node->prev = node->next; node->next = node->prev;
D. temp = node->next; node->next = node->prev; node->prev = temp;

Solution

  1. Step 1: Use a temporary variable to hold one pointer

    Store node->next in temp to avoid losing its value.
  2. Step 2: Swap pointers correctly

    Assign node->next to node->prev, then node->prev to temp.
  3. Final Answer:

    temp = node->next; node->next = node->prev; node->prev = temp; -> Option D
  4. Quick Check:

    Swap pointers using temp variable [OK]
Hint: Always use temp variable to swap pointers safely [OK]
Common Mistakes:
  • Swapping without temp loses pointer data
  • Assigning both pointers to same value
  • Mixing order of assignments
3. Given the following doubly linked list nodes and code snippet, what will be the new head node's data after reversal?
struct Node {
  int data;
  struct Node* next;
  struct Node* prev;
};

// List: 1 <-> 2 <-> 3 <-> NULL
struct Node* head = node1; // node1->data = 1

// Reversal code snippet:
struct Node* current = head;
struct Node* temp = NULL;
while (current != NULL) {
  temp = current->prev;
  current->prev = current->next;
  current->next = temp;
  current = current->prev;
}
if (temp != NULL) {
  head = temp->prev;
}
medium
A. 1
B. 3
C. 2
D. NULL

Solution

  1. Step 1: Trace pointer swaps in the loop

    Each node swaps its next and prev pointers, moving current backward through original list.
  2. Step 2: Update head after loop

    After loop, head is set to temp->prev, which points to original last node (data 3).
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    New head = original tail = 3 [OK]
Hint: New head is original tail after reversal [OK]
Common Mistakes:
  • Not updating head pointer after reversal
  • Confusing prev and next pointers
  • Assuming head remains same
4. Identify the error in the following code snippet that attempts to reverse a doubly linked list:
void reverse(struct Node** head_ref) {
  struct Node* current = *head_ref;
  struct Node* temp = NULL;
  while (current != NULL) {
    temp = current->next;
    current->next = current->prev;
    current->prev = temp;
    current = current->prev;
  }
  if (temp != NULL) {
    *head_ref = temp->prev;
  }
}
medium
A. temp should store current->prev instead of current->next
B. current should move to current->next instead of current->prev
C. The head pointer update is incorrect
D. No error, code is correct

Solution

  1. Step 1: Verify pointer swaps

    The next/prev swaps correctly reverse directions as current traverses the original next chain (now in prev).
  2. Step 2: Analyze temp assignment

    Temp should store current->prev before swapping, not current->next, to avoid losing pointer.
  3. Final Answer:

    temp should store current->prev instead of current->next -> Option A
  4. Quick Check:

    Correct temp assignment preserves traversal [OK]
Hint: Temp must hold current->prev before swapping pointers [OK]
Common Mistakes:
  • Incorrect head pointer update logic
  • Moving current incorrectly after swap
  • Not updating head pointer properly
5. You have a doubly linked list with nodes containing integer data. After reversing the list using the standard pointer swap method, which of the following statements is TRUE about the list's traversal?
hard
A. Traversing from the new head using next pointers visits nodes in original reverse order
B. Traversing from the new head using prev pointers visits nodes in original order
C. Traversing from the original head using next pointers visits nodes in reversed order
D. Traversing from the new head using next pointers visits nodes in original order

Solution

  1. Step 1: Understand reversal effect on pointers

    Reversing swaps next and prev pointers, so next pointers now point backward in original list.
  2. Step 2: Analyze traversal from new head

    Starting at new head, following next pointers visits nodes in original reverse order.
  3. Final Answer:

    Traversing from the new head using next pointers visits nodes in original reverse order -> Option A
  4. Quick Check:

    Next pointers after reverse = original prev [OK]
Hint: Next pointers after reverse follow original prev links [OK]
Common Mistakes:
  • Assuming next pointers still go forward after reverse
  • Confusing traversal directions
  • Thinking original head remains start
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