Bird
0
0
DSA Cprogramming

Reverse a Singly Linked List Iterative in DSA C

Choose your learning style9 modes available
Mental Model
We change the direction of each link in the list one by one until the whole list points backward.
Analogy: Imagine a line of people holding hands forward. We ask each person to turn around and hold the hand of the person behind them instead, one at a time.
head -> 1 -> 2 -> 3 -> null
↑curr
prev = null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null
Goal: Reverse the list so it becomes 3 -> 2 -> 1 -> null
Step 1: Set prev to null, curr to head (node 1)
prev = null, curr = 1 -> 2 -> 3 -> null
Why: We start with prev as null because the new tail will point to null
Step 2: Save next node (2), reverse curr's pointer to prev (null), move prev to curr (1), move curr to next (2)
1 -> null, prev = 1 -> null, curr = 2 -> 3 -> null
Why: We reverse the first link and move forward
Step 3: Save next node (3), reverse curr's pointer to prev (1), move prev to curr (2), move curr to next (3)
2 -> 1 -> null, prev = 2 -> 1 -> null, curr = 3 -> null
Why: We reverse the second link and continue
Step 4: Save next node (null), reverse curr's pointer to prev (2), move prev to curr (3), move curr to next (null)
3 -> 2 -> 1 -> null, prev = 3 -> 2 -> 1 -> null, curr = null
Why: We reverse the last link; curr is now null, so we stop
Result:
3 -> 2 -> 1 -> null
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Node structure
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// Function to create a new node
Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// Function to reverse the linked list iteratively
Node* reverseList(Node* head) {
    Node* prev = NULL; // previous node starts as null
    Node* curr = head; // current node starts at head
    while (curr != NULL) {
        Node* next = curr->next; // save next node
        curr->next = prev;      // reverse pointer
        prev = curr;            // move prev forward
        curr = next;            // move curr forward
    }
    return prev; // prev is new head
}

// Function to print the list
void printList(Node* head) {
    Node* temp = head;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("null\n");
}

int main() {
    // Create list 1 -> 2 -> 3 -> null
    Node* head = createNode(1);
    head->next = createNode(2);
    head->next->next = createNode(3);

    printf("Original list:\n");
    printList(head);

    // Reverse the list
    head = reverseList(head);

    printf("Reversed list:\n");
    printList(head);

    return 0;
}
Node* prev = NULL; // previous node starts as null Node* curr = head; // current node starts at head
Initialize pointers to start reversing
while (curr != NULL) {
Traverse until end of list
Node* next = curr->next; // save next node
Keep next node before changing pointer
curr->next = prev; // reverse pointer
Reverse current node's pointer to previous
prev = curr; // move prev forward curr = next; // move curr forward
Advance prev and curr to continue reversal
return prev; // prev is new head
Return new head after full reversal
OutputSuccess
Original list: 1 -> 2 -> 3 -> null Reversed list: 3 -> 2 -> 1 -> null
Complexity Analysis
Time: O(n) because we visit each node once to reverse pointers
Space: O(1) because we only use a few extra pointers, no extra list
vs Alternative: Compared to recursive reversal which uses O(n) space on call stack, iterative is more space efficient
Edge Cases
Empty list (head is NULL)
Function returns NULL immediately, no reversal needed
DSA C
while (curr != NULL) {
Single node list
Pointer reversal happens but list remains same, returns the single node
DSA C
while (curr != NULL) {
When to Use This Pattern
When asked to reverse a singly linked list, use the iterative pointer reversal pattern to efficiently reverse links in one pass.
Common Mistakes
Mistake: Not saving the next node before reversing pointer, causing loss of the rest of the list
Fix: Always save curr->next in a temporary variable before changing curr->next
Mistake: Returning the original head instead of the new head after reversal
Fix: Return prev pointer after loop ends, as it points to new head
Summary
Reverses the direction of a singly linked list by changing pointers iteratively.
Use when you need to reverse a linked list efficiently without extra space.
The key is to keep track of previous, current, and next nodes to reverse links safely.