3.1Singly Linked List
- 3.1.1What is a singly linked list?
- 3.1.2Creating a singly linked list
- 3.1.3Traversal
- 3.1.4Inserting at the end
- 3.1.5Inserting at a position
- 3.1.6Inserting at the head
- 3.1.7Deleting from the end
- 3.1.8Deleting from the head
- 3.1.9Deleting from the middle
- 3.1.10Searching for a value
- 3.1.11Reversing a linked list (iterative)
- 3.1.12Reversing a linked list (recursive)
Reversing a linked list (iterative)
Reversing a linked list means changing the direction of the links without moving any node. It is one of the most frequently asked linked list questions in interviews, because it tests careful work with pointers. This lesson covers the solution written with a loop (iterative).
1Idea
In the reversed list, the next field of every node must point to the node that used to come before it. The first node becomes the last and points to NULL; the old last node becomes the new head. The nodes are reversed in place, with no new memory.
We traverse the list once from start to end and keep three pointers. curr is the node whose link is being flipped. prev is the node the link will point to from now on; it starts as NULL. next saves the rest of the list before the link is flipped.
Each round runs four lines in order: save next, point curr->next at prev, move prev forward, move curr to next. When curr becomes NULL, every link has been flipped and prev points to the old last node. In the last step head is moved to that node.
On an empty list the loop never runs and head stays NULL. On a one-node list the node's next field ends up NULL again. Neither case needs extra code.
2Step by step
The drawing reverses the list 12 → 7 → 3. In each round one link changes direction; the changing link is orange. At each step the matching line of code lights up.
- Start. head points to 12; the links run left to right.
previs NULL andcurris the first node. The first node will be last in the reversed list, so its link must point toNULL.- The loop starts. Before flipping the link,
next=curr->next: 7 and everything after it are saved. - The link of 12 is turned to
prev, which is NULL. Thenprevmoves to 12 andcurrto 7. We could reach 7 only because ofnext. - Second round:
nextsaves 3, the link of 7 is turned to 12.prevmoves to 7 andcurrto 3. - Third round:
nextsaves NULL, the link of 3 is turned to 7.prevmoves to 3,currbecomesNULLand the loop ends. - head =
prev. The old last node, 3, is now the head of the list. - Result: 3 → 7 → 12. No node moved; only the links were turned around.
struct Node { int data; struct Node* next;}; void reverse(struct Node** head) { struct Node* prev = NULL; struct Node* curr = *head; while (curr != NULL) { struct Node* next = curr->next; // keep the rest curr->next = prev; // flip the link prev = curr; curr = next; } *head = prev;}class Node { int data; Node next; Node(int data) { this.data = data; }} Node reverse(Node head) { Node prev = null; Node curr = head; while (curr != null) { Node next = curr.next; // keep the rest curr.next = prev; // flip the link prev = curr; curr = next; } return prev; // the new head}class Node: def __init__(self, data): self.data = data self.next = None def reverse(head): prev = None curr = head while curr is not None: next = curr.next # keep the rest curr.next = prev # flip the link prev = curr curr = next return prev # the new headThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Each node is visited once and each round does a fixed number of assignments.
However long the list is, only three pointers are used.
The recursive version of the same task also takes O(n) time, but each call takes room on the call stack, so it uses O(n) extra memory.
4Common mistake
The most common mistake is not saving next before flipping the link:
curr->next = prev;curr = curr->next; // now points backwardsAs soon as the link is flipped, curr->next points to prev, not to the rest of the list. The second line sends curr backwards; in the first round it becomes NULL and the loop ends. The nodes after 12 can never be reached again. Always write next = curr->next before changing the link.
5Try it
When the loop ends, which pointer points to the new head?
What happens if this function is called on an empty list (head = NULL)?
Lessons you mark show up in the curriculum. This is kept on this device only.