/**
* 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) {
if(head==NULL)
return NULL;
ListNode *low = new ListNode(-1), *low_tail = low;
ListNode *high = new ListNode(-1), *high_tail = high;
ListNode *cur = head;
while(cur)
{
if(cur->val<x)
{
low_tail->next = cur;
cur = cur->next;
low_tail = low_tail->next;
low_tail->next = NULL;
}
else
{
high_tail->next = cur;
cur = cur->next;
high_tail = high_tail->next;
high_tail->next = NULL;
}
}
low_tail->next = high->next;
return low->next;
}
};
leetcode:Partition List
最新推荐文章于 2018-10-12 09:53:13 发布