86. Partition List 發表於 2023-02-13 | 分類於 leetcode problemsolution建立兩個指標分別收集大於等於x的節點與小於x的節點 123456789101112131415161718192021class Solution {public: ListNode* partition(ListNode* head, int x) { ListNode *smaller = new ListNode(-1), *a = smaller , *bigger = new ListNode(-1), *b = bigger; ListNode *p = head; while(p){ if(p->val >= x){ b->next = p; b=b->next; } else{ a->next = p; a=a->next; } p=p->next; } b->next = nullptr; a->next = bigger->next; return smaller->next; }}; analysis time complexity O(n) space complexity O(1)