Coding interview problems โบ Linked Lists
๐ Linked Lists Medium
Reorder List
Reorder List explained: find the middle, reverse the second half and interleave the two halves in place, O(n) time, O(1) space. Tested Python and a live visual.
Also searched as: reorder linked list L0 Ln L1 Ln-1 ยท fold a linked list ยท interleave first and reversed second half
Try it live Free
Linked Lists: Reverse & Floyd's Cycle
Practise the reverse mode in the linked list lab until prev, cur and next feel automatic, since step 2 of this problem is exactly that reversal.
The problem
Given a linked list L0 -> L1 -> ... -> Ln, rearrange its nodes in place into L0 -> Ln -> L1 -> Ln-1 -> L2 -> ..., alternating from the front and the back. Change the links, not the node values.
The key insight
The pattern takes from the front and the back alternately, but a singly linked list cannot walk backwards. So make the back half walk forwards: split the list at its middle, reverse the second half, and then zip the two halves together one node from each side at a time. It is three classic moves glued together.
Step-by-step approach
- Find the end of the first half with slow and fast pointers (stop when
fast.nextorfast.next.nextisNone). - Cut the list after
slowand reverse the second half. - Take
first = headandsecond= head of the reversed half. - While
secondexists, save bothnextpointers, linkfirst -> second -> first.next, and advance both. - The first half is equal in length or one node longer, so it absorbs the last node naturally.
Python solution
class ListNode:
def __init__(self, val=0, next=None):
self.val, self.next = val, next
def reorder_list(head):
if not head or not head.next:
return head
slow, fast = head, head # 1. find end of first half
while fast.next and fast.next.next:
slow, fast = slow.next, fast.next.next
prev, cur = None, slow.next # 2. reverse the second half
slow.next = None
while cur:
cur.next, prev, cur = prev, cur, cur.next
first, second = head, prev # 3. interleave
while second:
n1, n2 = first.next, second.next
first.next, second.next = second, n1
first, second = n1, n2
return headComplexity
Finding the middle, reversing and merging are each one pass over at most n nodes, and only a handful of pointers are used.
Common mistakes
- Forgetting to cut the list (
slow.next = None), which leaves a cycle after the halves are interleaved. - Choosing the second middle for even lengths, which makes the second half longer than the first and drops or misplaces a node during the merge.
- Overwriting
first.nextbefore saving it, which loses the rest of the first half.
Frequently asked questions
What is the time and space complexity of Reorder List?
O(n) time with three linear passes, and O(1) extra space because all changes are pointer rewires in place.
Why reverse the second half instead of using a stack?
A stack of the second-half nodes also works and is easy to reason about, but costs O(n) extra space. Reversing in place gets the same back-to-front order in O(1) space.
Why stop the fast pointer at fast.next.next?
That leaves slow on the first middle, so the first half is the same length as the second or one node longer, which is exactly what the interleaving loop expects.
Which problems reuse these building blocks?
Palindrome Linked List uses find-middle plus reverse, and merge sort on a linked list uses find-middle plus merge.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Linked Lists Linked Lists: Reverse & Floyd's Cycle lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.