← Home

86. Partition List

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
86

This is one of those problems where the clean idea matters more than the amount of code. For 86. Partition List, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

The notes already sitting in the source point us in the right direction:

  • time: O(N),

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are partition.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(N),
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 12 ms, faster than 13.34% of C++ online submissions for Partition List.
02//Memory Usage: 10.6 MB, less than 14.01% of C++ online submissions for Partition List.
03//time: O(N), 
04/**
05 * Definition for singly-linked list.
06 * struct ListNode {
07 *     int val;
08 *     ListNode *next;
09 *     ListNode() : val(0), next(nullptr) {}
10 *     ListNode(int x) : val(x), next(nullptr) {}
11 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
12 * };
13 */
14class Solution {
15public:
16    ListNode* partition(ListNode* head, int x) {
17        ListNode *smallhead, *largehead;
18        smallhead = new ListNode(-1);
19        largehead = new ListNode(-1);
20        
21        ListNode *cur = head;
22        ListNode *smallcur = smallhead, *largecur = largehead;
23        
24        while(cur){
25            if(cur->val < x){
26                smallcur->next = cur;
27                smallcur = smallcur->next;
28            }else{
29                largecur->next = cur;
30                largecur = largecur->next;
31            }
32            cur = cur->next;
33        }
34        
35        largecur->next = nullptr;
36        smallcur->next = largehead->next;
37        
38        return smallhead->next;
39    }
40};

Cost

Complexity

Time
O(N),
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.