s4553711
7/21/2017 - 3:16 PM

86.cpp

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        ListNode *G = new ListNode(0);
        ListNode *L = new ListNode(0);
        ListNode *less = L;
        ListNode *great = G;
        while(head) {
            if (head->val < x)
                less = less->next = head;
            else
                great = great->next = head;
            head = head->next;
        }
        great->next = NULL;
        less->next = G->next;
        
        return L->next;
    }
};