Bird
0
0
DSA Cprogramming~20 mins

Reverse a Singly Linked List Recursive in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Recursive Reverse Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Recursive Reverse on a 3-Node List
What is the printed state of the linked list after calling the recursive reverse function on this list?

Initial list: 1 -> 2 -> 3 -> null
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

Node* reverseRecursive(Node* head) {
    if (head == NULL || head->next == NULL) {
        return head;
    }
    Node* rest = reverseRecursive(head->next);
    head->next->next = head;
    head->next = NULL;
    return rest;
}

// After calling reverseRecursive on list 1->2->3->NULL, print the list
A3 -> 2 -> 1 -> null
B1 -> 2 -> 3 -> null
C3 -> 1 -> 2 -> null
D2 -> 3 -> 1 -> null
Attempts:
2 left
💡 Hint
Think about how the recursion reaches the last node and then reverses the links step by step.
Predict Output
intermediate
2:00remaining
Output of Recursive Reverse on Single Node List
What is the printed state of the linked list after calling the recursive reverse function on a single-node list?

Initial list: 5 -> null
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

Node* reverseRecursive(Node* head) {
    if (head == NULL || head->next == NULL) {
        return head;
    }
    Node* rest = reverseRecursive(head->next);
    head->next->next = head;
    head->next = NULL;
    return rest;
}

// After calling reverseRecursive on list 5->NULL, print the list
Anull
B5 -> null
C5 -> 5 -> null
DError: segmentation fault
Attempts:
2 left
💡 Hint
A single node reversed is still the same node.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Recursive Reverse Function
What error will this recursive reverse function cause when called on a list with multiple nodes?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

Node* reverseRecursive(Node* head) {
    if (head == NULL || head->next == NULL) {
        return head;
    }
    Node* rest = reverseRecursive(head->next);
    head->next = NULL;
    head->next->next = head;
    return rest;
}
ASegmentation fault (runtime error)
BNo error, works correctly
CCompilation error: invalid assignment
DInfinite recursion
Attempts:
2 left
💡 Hint
Look carefully at the order of pointer assignments after recursion.
Predict Output
advanced
2:00remaining
Output of Recursive Reverse on 4-Node List
What is the printed state of the linked list after calling the recursive reverse function on this list?

Initial list: 10 -> 20 -> 30 -> 40 -> null
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

Node* reverseRecursive(Node* head) {
    if (head == NULL || head->next == NULL) {
        return head;
    }
    Node* rest = reverseRecursive(head->next);
    head->next->next = head;
    head->next = NULL;
    return rest;
}

// After calling reverseRecursive on list 10->20->30->40->NULL, print the list
A40 -> 20 -> 30 -> 10 -> null
B10 -> 20 -> 30 -> 40 -> null
C40 -> 30 -> 20 -> 10 -> null
D30 -> 40 -> 20 -> 10 -> null
Attempts:
2 left
💡 Hint
The recursive function reverses the list by changing the next pointers step by step from the end.
🧠 Conceptual
expert
2:00remaining
Why Does Recursive Reverse Use head->next->next = head?
In the recursive reverse function for a singly linked list, why do we assign 'head->next->next = head;' instead of 'head->next = head;'?
ABecause 'head->next->next = head;' sets the current node's next to NULL automatically.
BBecause 'head->next = head;' is invalid syntax in C and causes a compilation error.
CBecause 'head->next = head;' deletes the next node from memory.
DBecause 'head->next->next = head;' reverses the link direction, making the next node point back to the current node, while 'head->next = head;' would create a cycle.
Attempts:
2 left
💡 Hint
Think about how pointers must be reversed to flip the list direction.