您的位置:首页 > 其它

leetcode:Partition List

2015-04-01 16:07 239 查看
/**
* 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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: