Of course. Reversing a singly linked list in place is a classic computer science problem. The key is to iterate through the list, changing the next pointer of each node to point to its previous node instead of its next one.
The Core Idea: The Three-Pointer Approach
To do this without using extra memory (i.e., "in place"), we can iterate through the list using three pointers:
prev: Points to the previous node. It will start as NULL because the first node of the original list will become the last node of the new list (and its next pointer should be NULL).
current: Points to the node we are currently processing. It will start at the head of the list.
next_node: A temporary pointer to store the next node in the original list before we change the current->next pointer. This is crucial so we don't lose the rest of the list.
Step-by-Step Walkthrough
Let's imagine we have the list: A -> B -> C -> NULL
Initial State:
prev = NULL
current = A
head points to A
Iteration 1 (Processing Node A):
- Store next:
next_node = current->next (so next_node points to B).
- Reverse the pointer:
current->next = prev (so A->next now points to NULL).
- Move pointers forward:
prev = current (so prev now points to A).
current = next_node (so current now points to B).
State after Iteration 1:
NULL <- A
prev is A
current is B
- The rest of the original list is still intact starting from
B: B -> C -> NULL
Iteration 2 (Processing Node B):
- Store next:
next_node = current->next (so next_node points to C).
- Reverse the pointer:
current->next = prev (so B->next now points to A).
- Move pointers forward:
prev = current (so prev now points to B).
current = next_node (so current now points to C).
State after Iteration 2:
NULL <- A <- B
prev is B
current is C
- The rest of the original list is
C -> NULL
Iteration 3 (Processing Node C):
- Store next:
next_node = current->next (so next_node points to NULL).
- Reverse the pointer:
current->next = prev (so C->next now points to B).
- Move pointers forward:
prev = current (so prev now points to C).
current = next_node (so current now points to NULL).
State after Iteration 3:
NULL <- A <- B <- C
prev is C
current is NULL
End of Loop:
The loop terminates because current is now NULL. The new head of the reversed list is the last node we processed, which is pointed to by prev. We return prev.
Complete C Implementation
Here is a full, runnable C program that demonstrates the concept.
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the singly linked list
typedef struct Node {
int data;
struct Node* next;
} Node;
// --- Function Prototypes ---
Node* createNode(int data);
void printList(Node* head);
Node* reverseList(Node* head);
void freeList(Node* head);
int main() {
// Create a sample linked list: 1 -> 2 -> 3 -> 4 -> NULL
Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
printf("Original list: ");
printList(head);
// Reverse the list and update the head pointer
head = reverseList(head);
printf("Reversed list: ");
printList(head);
// Free the allocated memory
freeList(head);
return 0;
}
/**
* @brief Reverses a singly linked list in place.
*
* @param head A pointer to the head of the list.
* @return A pointer to the new head of the reversed list.
*/
Node* reverseList(Node* head) {
// If the list is empty or has only one node, no change is needed.
if (head == NULL || head->next == NULL) {
return head;
}
Node* prev = NULL;
Node* current = head;
Node* next_node = NULL;
while (current != NULL) {
// 1. Store the next node before we overwrite current->next
next_node = current->next;
// 2. Reverse the pointer of the current node
current->next = prev;
// 3. Move the pointers one position ahead for the next iteration
prev = current;
current = next_node;
}
// After the loop, 'prev' will be pointing to the new head of the list.
return prev;
}
// --- Helper Functions ---
/**
* @brief Creates a new node with the given data.
*/
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation failed!\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
/**
* @brief Prints the linked list from head to tail.
*/
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
/**
* @brief Frees all the nodes in the list to prevent memory leaks.
*/
void freeList(Node* head) {
Node* current = head;
Node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
}
How to Compile and Run
- Save the code as
reverse_list.c.
- Compile it using a C compiler like GCC:
gcc reverse_list.c -o reverse_list
- Run the executable:
./reverse_list
Expected Output:
Original list: 1 -> 2 -> 3 -> 4 -> NULL
Reversed list: 4 -> 3 -> 2 -> 1 -> NULL
Time and Space Complexity
- Time Complexity: O(n), where
n is the number of nodes in the list. We need to visit each node exactly once to change its next pointer.
- Space Complexity: O(1). This is the main advantage of the in-place algorithm. We only use a fixed number of pointers (
prev, current, next_node) regardless of the size of the list. No extra memory proportional to the list size is allocated.