Opening your learning path
Progress is saved as you study.
Opening your learning path
Progress is saved as you study.
The Reordering / Partitioning pattern applies when you need the Reordering / Partitioning technique within the Linked List Manipulation Patterns pattern. Its time complexity is O(n) and space complexity O(1). It is used in 6 problems on Thita, including Insert Greatest Common Divisors in Linked List, Odd Even Linked List and Partition List. Common variations are In-place Reversal and Merging Two Sorted Lists.
Learn to reorder and partition linked lists. Master techniques like swap pairs, rotate list, and odd-even separation.
Reordering / Partitioning is one of the 5 subpatterns of the Linked List Manipulation Patterns pattern, which covers in-place reversal, merging sorted lists, reordering, and intersection detection. The whole pattern is about 2 hours of study. This subpattern is an important variation worth recognising on sight.
Master linked list manipulation including in-place reversal, merging sorted lists, detecting intersections, and reordering. Essential pointer manipulation techniques for interviews. Problems in this subpattern are usually searched for as reorder list, partition list, swap pairs, rotate list, odd even linked list, leetcode 143.
Read the theory for Reordering / Partitioning, then work the problems attached to it in the browser editor. Your solution runs against the problem's test cases, and the AI coach offers a hint about the technique you are missing rather than a finished solution. Progress is tracked per subpattern, so the Linked List Manipulation Patterns tracker shows this one as covered once you have solved its problems.
Reordering / Partitioning is one lesson in a 16-pattern DSA course. If you are preparing end to end, work the interview-critical patterns first and use the pattern sheet as the checklist; if you are here for one technique, the Linked List Manipulation Patterns guide is the shortest path back to the rest of it.
6 problems, easiest first.
Imagine you have a line of people waiting for a ride at an amusement park. Sometimes, you might want to rearrange the line: maybe group all the kids together, or alternate between adults and kids, or even rotate the line so the last few people go to the front. This is exactly what the Reordering / Partitioning subpattern is about—rearranging the nodes (people) in a linked list (the line) based on certain rules, without losing anyone or mixing up their order unnecessarily.
This pattern is especially useful when you need to:
In real life, think of organizing books on a shelf by genre, or rotating a playlist so your favorite song plays first. In linked lists, this means moving nodes around—without creating new nodes or losing connections. This subpattern is common in coding interviews at top tech companies like Amazon, Google, and Facebook, because it tests your understanding of pointers, edge cases, and efficient in-place manipulation.
Let’s break down the key ideas behind reordering and partitioning linked lists:
next pointers, not by creating new nodes or copying values. This is like rearranging chairs in a room without bringing in new ones.By mastering these concepts, you'll be able to tackle a wide range of linked list reordering and partitioning problems with confidence.
Let’s build a mental model for solving reordering/partitioning linked list problems:
By following these steps, you’ll have a reliable approach for any reordering or partitioning linked list problem.
Let’s look at two well-commented C++ code examples based on the database problems:
1// Definition for singly-linked list.
2struct ListNode {
3 int val;
4 ListNode *next;
5 ListNode(int x) : val(x), next(nullptr) {}
6};
7
8// Partition List: Rearrange nodes so that all nodes < x come before nodes >= x
9ListNode* partition(ListNode* head, int x) {
10 // Dummy heads for two separate lists
11 ListNode* lessHead = new ListNode(0); // List of nodes < x
12 ListNode* greaterHead = new ListNode(0); // List of nodes >= x
13
14 ListNode* less = lessHead; // Tail pointer for less list
15 ListNode* greater = greaterHead; // Tail pointer for greater list
16
17 // Traverse the original list
18 while (head != nullptr) {
19 if (head->val < x) {
20 less->next = head; // Add to less list
21 less = less->next;
22 } else {
23 greater->next = head; // Add to greater list
24 greater = greater->next;
25 }
26 head = head->next; // Move to next node
27 }
28
29 greater->next = nullptr; // End the greater list
30 less->next = greaterHead->next; // Connect less list to greater list
31
32 ListNode* result = lessHead->next; // Skip dummy node
33 delete lessHead;
34 delete greaterHead;
35 return result;
36}Explanation:
x, and one for nodes greater than or equal to x.1// Swap every two adjacent nodes in the list
2ListNode* swapPairs(ListNode* head) {
3 // Dummy node simplifies edge cases (e.g., swapping at the head)
4 ListNode dummy(0);
5 dummy.next = head;
6 ListNode* prev = &dummy;
7
8 while (head && head->next) {
9 ListNode* first = head;
10 ListNode* second = head->next;
11
12 // Swapping nodes
13 prev->next = second; // prev points to second node
14 first->next = second->next; // first points to node after second
15 second->next = first; // second points to first (swap complete)
16
17 // Move pointers forward for next swap
18 prev = first;
19 head = first->next;
20 }
21 return dummy.next;
22}Explanation:
next pointers.Let’s walk through "Partition List" step by step:
Problem Recap:
Given a linked list and a value x, rearrange the list so that all nodes with values less than x come before nodes with values greater than or equal to x. Preserve the original order within each group.
Visualize the Goal:
Imagine sorting a deck of cards into two piles: all cards less than a certain number in one pile, the rest in another. But you must keep the original order within each pile.
How do we do this in a linked list?
Implementation Steps:
Edge Cases:
Why does this work?
Final Touch:
This approach is elegant and efficient, and the use of dummy nodes makes the code clean and easy to follow.
Signals to Recognize This Pattern:
Aha! Moments:
Debugging Tips:
Memory Tricks:
With practice, you’ll spot these patterns quickly and solve them with confidence!