</>

143. Reorder List

Medium NeetCode 150
Linked List Time O(N) Space O(1)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Linked List
Time
O(N)
Space
O(1)

Standard Solution (C++)

SummaryGiven a singly-linked list, rearrange its nodes so that the list interleaves the first half with the reversed second half (e.g., L0 → Ln → L1 → Ln-1 → …), modifying it in-place without returning anything.

Approach
  • Find the midpoint using slow/fast pointers so the list can be split into two halves.
  • Reverse the second half in-place with a standard three-pointer reversal, making the tail the new head of the back segment.
  • Merge the two halves by alternating nodes: take one from the front, then one from the (reversed) back, until the shorter half is exhausted.
  • All three phases are O(n) and use only pointer variables, so the overall approach is linear time and constant extra space.
#include <cstddef>
using namespace std;

// Definition for singly-linked list.
struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    void reorderList(ListNode* head) {
        if (!head || !head->next) return;

        // --- Phase 1: find the end of the first half ---
        // When the loop exits, `slow` sits at the last node of the first half.
        ListNode* slow = head;
        ListNode* fast = head->next;
        while (fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
        }

        // --- Phase 2: reverse the second half in-place ---
        ListNode* second = slow->next;
        slow->next = nullptr;   // detach first half from second

        ListNode* prev = nullptr;
        while (second) {
            ListNode* nxt = second->next;
            second->next = prev;
            prev = second;
            second = nxt;
        }
        // `prev` is now the head of the reversed second half

        // --- Phase 3: interleave first half with reversed second half ---
        ListNode* p1 = head;
        ListNode* p2 = prev;
        while (p2) {                    // second half is always <= first half length
            ListNode* n1 = p1->next;
            ListNode* n2 = p2->next;
            p1->next = p2;
            p2->next = n1;
            p1 = n1;
            p2 = n2;
        }
    }
};
Time O(n) Space O(1)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)


// Definition for singly-linked list.
struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    void reorderList(ListNode* head) {
        if(!head || !head->next){
            return;
        }
        ListNode* slow=head;
        ListNode* fast=head->next;

        // find mid
        // slow: 1 2
        // fast: 2 4
        while(fast && fast->next){
            slow=slow->next;
            fast=fast->next->next;
        }

        ListNode* pre=nullptr;
        ListNode* cur=slow->next;
        ListNode* nxt=cur->next;

        slow->next=nullptr;
        while(nxt){
            cur->next=pre;
            pre=cur;
            cur=nxt;
            nxt=nxt->next;
        }
        cur->next=pre;

        ListNode* list1=head;
        ListNode* list2=cur;
        ListNode* tmp1;
        ListNode* tmp2;

        while(list2){
            tmp1=list1->next;
            tmp2=list2->next;
            list1->next=list2;
            list2->next=tmp1;
            list1=tmp1;
            list2=tmp2;
        }
    }
};

// Time Complexity: O(N)
// Space Complexity: O(1)
Time O(N) Space O(1)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern