/**
* 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* smallHead = new ListNode(-1);
ListNode* smallCur = smallHead;
ListNode* bigHead = new ListNode(-1);
ListNode* bigCur = bigHead;
ListNode* curNode = head;
while(curNode != NULL){
if(curNode->val < x){
smallCur ->next = new ListNode(curNode->val);
smallCur = smallCur->next;
}
else{
bigCur->next = new ListNode(curNode->val);
bigCur=bigCur->next;
}
curNode = curNode->next;
}
smallCur->next = bigHead->next;
return smallHead->next;
}
};
要点:1 新建的头节点,只用来做引导,最后对接的时候用,没有实际的值,最后的返回值,是从新建的头节点的下一个开始
2 ListNode全部采用指针形式,所以运算符用->
3 新增节点,必须用new运算符:List Node* bigHead = new ListNode(-1);