/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
  public:
    ListNode* partition(ListNode* pHead, int x) {
        // write code here
        if (!pHead || !pHead->next) return pHead;
        ListNode dummySmall(0);
        ListNode dummyLarge(0);

        ListNode* pSmall = &dummySmall;
        ListNode* pLarge = &dummyLarge;
 
        while (pHead) {
            if (pHead->val < x) {
                pSmall->next = pHead;
                pSmall = pSmall->next;
            } else {
                pLarge->next = pHead;
                pLarge = pLarge->next;
            }
            pHead = pHead->next;
        }
        pLarge->next = nullptr;
        pSmall->next = dummyLarge.next;
        return dummySmall.next;
    }
};