C Program
struct Node { int val; struct Node* next; }; struct Node* reverse(struct Node* head) { struct Node *prev = NULL, *next; while (head) { next = head->next; head->next = prev; prev = head; head = next; } return prev; }
C Output
Input: 1 → 2 → 3 → NULL Output: 3 → 2 → 1 → NULL
C++ Program
struct Node { int val; Node* next; Node(int x) : val(x), next(nullptr) {} }; Node* reverse(Node* head) { Node *prev = NULL, *next; while (head) { next = head->next; head->next = prev; prev = head; head = next; } return prev; }
C++ Output
Input: 10 → 20 → 30 Output: 30 → 20 → 10
JAVA Program
class Node { int val; Node next; Node(int x) { val = x; } } Node reverse(Node head) { Node prev = null, next; while (head != null) { next = head.next; head.next = prev; prev = head; head = next; } return prev; }
JAVA Output
Input: 1 → 2 → 3 Output: 3 → 2 → 1
Python Program
class Node: def __init__(self, val): self.val = val self.next = None def reverse(head): prev = None while head: nxt = head.next head.next = prev prev = head head = nxt return prev
Python Output
Input: 5 → 6 → 7 Output: 7 → 6 → 5
In-Depth Explanation
Example
Suppose you have a linked list such as: 1 → 2 → 3 → NULL. You need to reverse the pointers so it is 3 → 2 → 1 → NULL. With each step, you take the current node, point its next pointer to the previous node, and move forward. By the end, what was the previous head becomes the new head.
Real-Life Analogy
Imagine a train with more than one coach. To reverse the direction of the train doesn't shift the coaches physically but, instead, reverses the order they are accessed. If the train was traveling from A → B → C, reversing it means now it travels from C → B → A, similar to reversing the pointers in a linked list.
Another metaphor: A paperclip chain linked end to end. You unplug them individually and reconnect them in the opposite order.
Why It Matters
Reversing a linked list is the building block for most higher-level linked list problems such as reversing in chunks of K, palindrome checking, flattening, detecting a cycle, and others. If you can't reverse a list, you can't really work with it. It also helps you practice pointer manipulation and memory referencing — fundamental subjects in data structures and low-level programming.
What You Learn from This
This exercise conditions your mind to consider pointer redirection, which is a core programming skill. You learn to keep former, current, and future pointers with care so that you don't lose access to the remaining part of the list. You also understand how references to memory work in linear structures, which is crucial to understanding heap, stack, and memory leaks.
Interview Relevance and Real Projects
This is a top 10 timeless interview question posed by Amazon, Google, and TCS. Interviewers usually request you to reverse the list in-place, recursively, or partially (e.g., for first k nodes). In practical use cases, it's applied in undo operations, stack simulation, and backward navigation systems (such as browsing history or reversing playback queues).
SEO-Optimized Explanation
Reversing a linked list is among the most necessary and popular coding interview problems and data structures. The algorithm does this by repeatedly reassigning pointers such that the bottom-most element is now the head, and connections between nodes are reversed. This operation is core to answering many linked list-type questions, ranging from reversing in chunks to verifying palindromic forms. Through the understanding of reverse linked list logic in C, C++, Java, and Python, developers develop a clear insight into memory handling, pointer management, and list manipulation, making it an essential chapter for all beginners as well as professionals involved in real-world software development.
Social Plugin