My solution ,very easy


  • 0
    P
    class Solution {
    public:
    ListNode* partition(ListNode* head, int x) {
    	ListNode dummy1(0);
    	ListNode dummy2(0);
    	ListNode *less = &dummy1;
    	ListNode *big = &dummy2;
    	while(head)
    	{
    		if(head->val < x)
    		{
    			less->next = head;
    			less = less->next;
    		}
    		else
    		{
    			big->next = head;
    			big = big->next;
    		}
    		head = head->next;
    	}
    	less->next = dummy2.next;
        big->next = NULL;
    	return dummy1.next;
    }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.