Bird
0
0
DSA Cprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked List Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of reversing a linked list with 3 nodes
What is the printed output of the linked list after reversing it iteratively?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

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

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

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

int main() {
    Node* head = malloc(sizeof(Node));
    head->data = 1;
    head->next = malloc(sizeof(Node));
    head->next->data = 2;
    head->next->next = malloc(sizeof(Node));
    head->next->next->data = 3;
    head->next->next->next = NULL;

    head = reverseList(head);
    printList(head);
    return 0;
}
A3 -> 1 -> 2 -> NULL
B1 -> 2 -> 3 -> NULL
CNULL
D3 -> 2 -> 1 -> NULL
Attempts:
2 left
💡 Hint
Think about how the pointers change direction in the iterative reversal.
Predict Output
intermediate
2:00remaining
Output after reversing a single-node linked list
What is the output when reversing a linked list with only one node?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

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

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

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

int main() {
    Node* head = malloc(sizeof(Node));
    head->data = 42;
    head->next = NULL;

    head = reverseList(head);
    printList(head);
    return 0;
}
A42 -> 42 -> NULL
BNULL
C42 -> NULL
DError: segmentation fault
Attempts:
2 left
💡 Hint
Reversing a single node list should not change the list.
Predict Output
advanced
2:00remaining
Output of reversing a linked list with a cycle
What happens when you run the iterative reverse function on a linked list that contains a cycle?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

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

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

int main() {
    Node* head = malloc(sizeof(Node));
    Node* second = malloc(sizeof(Node));
    Node* third = malloc(sizeof(Node));
    head->data = 1;
    head->next = second;
    second->data = 2;
    second->next = third;
    third->data = 3;
    third->next = second; // cycle here

    head = reverseList(head);
    printf("Reversed list printed\n");
    return 0;
}
AProgram runs infinitely (infinite loop)
BNULL
C3 -> 2 -> 1 -> NULL
DSegmentation fault error
Attempts:
2 left
💡 Hint
Think about what happens when the list has a cycle and the loop condition depends on current != NULL.
Predict Output
advanced
2:00remaining
Final state of pointers after one iteration of reversal
Given the initial linked list 1 -> 2 -> 3 -> NULL, what are the values of prev, current, and next pointers after the first iteration of the reversal loop?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

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

int main() {
    Node* head = malloc(sizeof(Node));
    head->data = 1;
    head->next = malloc(sizeof(Node));
    head->next->data = 2;
    head->next->next = malloc(sizeof(Node));
    head->next->next->data = 3;
    head->next->next->next = NULL;

    Node* prev = NULL;
    Node* current = head;
    Node* next = NULL;

    // First iteration of reversal loop
    next = current->next;
    current->next = prev;
    prev = current;
    current = next;

    printf("prev: %d, current: %d, next: %d\n", prev->data, current->data, next->data);
    return 0;
}
Aprev: NULL, current: 1, next: 2
Bprev: 1, current: 2, next: 2
Cprev: 2, current: 3, next: 3
Dprev: 1, current: 3, next: 3
Attempts:
2 left
💡 Hint
Trace the pointer changes step by step for the first iteration.
🧠 Conceptual
expert
2:00remaining
Why iterative reversal is preferred over recursive for large lists
Which is the main reason iterative reversal of a singly linked list is preferred over recursive reversal for very large lists?
ARecursive reversal uses extra stack space which can cause stack overflow for large lists
BIterative reversal is slower than recursive reversal for large lists
CRecursive reversal cannot reverse lists with cycles
DIterative reversal requires more memory than recursive reversal
Attempts:
2 left
💡 Hint
Think about how recursion uses memory for each function call.