aboutsummaryrefslogtreecommitdiff
path: root/partition_list.cpp
blob: b2e44a95abdff8133b2c6ca5cb33408bceb78103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "leetcode.h"

class Solution {
public:
  ListNode *partition(ListNode *head, int x) {
    ListNode *fTmp = new ListNode(0), *bTmp = new ListNode(0), *front = fTmp,
             *back = bTmp, *curr = head;
    while (curr) {
      if (curr->val < x) {
        front->next = curr;
        front = curr;
      } else {
        back->next = curr;
        back = curr;
      }
    }
    front->next = bTmp->next, back->next = nullptr;
    return fTmp->next;
  }
};