/**
* 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.