/**
 * struct ListNode {
 *    int val;
 *    struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 
     * @param x int整型 
     * @return ListNode类
     */
    ListNode* partition(ListNode* head, int x) {
        // write code here
        ListNode* lessDummyHead = new ListNode(-1);
        ListNode* moreDummyHead = new ListNode(-1);
        ListNode* p = lessDummyHead;
        ListNode* q = moreDummyHead;
        while(head != NULL){
            if(head -> val < x){
                p -> next = head;
                p = p -> next;
            }else{
                q -> next = head;
                q = q -> next;
            }
            head = head -> next;
        }
        p -> next = moreDummyHead -> next;
        q -> next = NULL;
        p = lessDummyHead -> next;
        delete lessDummyHead;
        delete moreDummyHead;
        return p;
    }
};