You are given the head of a singly linked-list. The list can be represented as:
L0 → L1 → … → Ln - 1 → Ln
Reorder the list to be on the following form:
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
You may not modify the values in the list's nodes. Only nodes themselves may be changed.
Example 1:
Input: head = [1,2,3,4] Output: [1,4,2,3]
Example 2:
Input: head = [1,2,3,4,5] Output: [1,5,2,4,3]
Constraints:
[1, 5 * 104].1 <= Node.val <= 1000Problem Overview: You are given the head of a singly linked list. The goal is to reorder nodes so the list follows the pattern L0 → Ln → L1 → Ln-1 → L2 → Ln-2 .... The structure must be modified in-place without changing node values—only pointers can be rearranged.
Approach 1: Using Array List (O(n) time, O(n) space)
This approach copies all nodes into an array-like structure so you can access both ends easily. First iterate through the linked list and push each node into an ArrayList or dynamic array. Then use two pointers: one starting at the beginning and one at the end. Alternate linking nodes from the front and back while moving the pointers inward. Because random access is O(1), the reordering becomes straightforward. The tradeoff is extra memory proportional to the number of nodes. This approach is simple to reason about and helpful when first understanding pointer rearrangement in a linked list.
Approach 2: Reverse Second Half and Merge (O(n) time, O(1) space)
This is the optimal in-place technique and the one most interviewers expect. First find the middle of the list using the classic slow and fast pointer strategy from two pointers. Once the midpoint is located, reverse the second half of the list. Now you have two lists: the original first half and a reversed second half. Finally merge them by alternating nodes—take one node from the first half, then one from the reversed half, and repeat until the lists are exhausted.
The key insight is that reversing the second half allows you to access the last nodes in forward order without using extra memory. Reversal itself runs in O(n) time and constant space by iteratively updating next pointers. During the merge phase, pointers are carefully rewired so nodes interleave correctly without losing references. This technique relies purely on pointer manipulation and avoids auxiliary structures like a stack or array.
Recommended for interviews: The reverse-and-merge approach is the expected solution because it runs in O(n) time and O(1) extra space. Interviewers often want to see if you can combine multiple linked list techniques: finding the middle, reversing a list, and merging two lists. Starting with the array approach demonstrates the core idea, but implementing the in-place method shows stronger mastery of pointer manipulation.
This approach involves splitting the list into two halves, reversing the second half, and then merging the two lists alternately.
The C solution first finds the midpoint using two pointers, splits the list, reverses the second half, and then merges the first half with the reversed second half node by node.
Time Complexity: O(n).
Space Complexity: O(1), as the solution modifies the list in place.
This method involves storing the linked list nodes in an array list and then rearranging their connections to achieve the desired order.
The C++ solution uses an array to store nodes and then reorganizes them using two pointers, one from the start and one from the end of the list, alternately linking them.
Time Complexity: O(n).
Space Complexity: O(n) due to the array storage.
We first use fast and slow pointers to find the midpoint of the linked list, then reverse the second half of the list, and finally merge the two halves.
The time complexity is O(n), where n is the length of the linked list. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
Rust
JavaScript
C#
| Approach | Complexity |
|---|---|
| Reverse Second Half and Merge | Time Complexity: O(n). |
| Using Array List | Time Complexity: O(n). |
| Fast and Slow Pointers + Reverse List + Merge Lists | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Using Array List | O(n) | O(n) | Simpler implementation when extra memory is acceptable |
| Reverse Second Half and Merge | O(n) | O(1) | Optimal interview solution with constant extra space |
Linkedin Interview Question - Reorder List - Leetcode 143 - Python • NeetCode • 436,011 views views
Watch 9 more video solutions →Practice Reorder List with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor