这题1刷只想到用新的点,2刷试试不用新的点,直接在原链表中运行。
Ps 链表的申请 复习
/**
* 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 *p = new ListNode(0);
ListNode *q = new ListNode(0);
ListNode *ph = p;
ListNode *qh = q;
while(head != NULL){
if(head -> val < x){
p -> next = head;
p = p -> next;
}
else{
q -> next = head;
q = q -> next;
}
head = head -> next;
}
p -> next = qh -> next;
q -> next = NULL;
return ph -> next;
}
};