/**
* 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) {
ListNode l, r;
ListNode* trans = &l, *trans1 = &r;
ListNode* p = head;
while(p){
if(p ->val < x){
trans ->next = p;
trans = p;
}else{
trans1 ->next = p;
trans1 = p;
}
p = p ->next;
}
trans1 ->next = nullptr; //不能少,不然链表是一个环
if(trans != nullptr) trans -> next = r.next;
return l.next;
}
};
07-29
249