/**主要思想就是將原來的鏈表拆成2個,一個全部小於閾值,一個大於或者等於閾值,然後將後者鏈接到前者後面;
* 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*s = new ListNode(0),*q=new ListNode(0);
ListNode *p=s,*r=q;
while (head!= NULL)
{
if (head->val < x)
{
s->next = head;
s = s->next;
}
else
{
q->next = head;
q = q->next;
}
head = head->next;
}
q->next = NULL;
s->next = r->next;
delete r;
return p->next;
}
};
86. Partition List
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.