/**
* 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* cur = head;
ListNode*dn1 = new ListNode(-1);
ListNode*dn2 = new ListNode(-1);
ListNode* cur1 = dn1;
ListNode* cur2 = dn2;
while (cur) {
if (cur->val < x) {
cur1->next = cur;
cur = cur->next;
# 同时跟进
cur1 = cur1->next;
# 将新添的节点与原链表中之后的节点断开
cur1->next = NULL;
}
else {
cur2->next = cur;
cur = cur->next;
cur2 = cur2->next;
cur2->next = NULL;
}
}
cur1->next = dn2->next;
return dn1->next;
}
};
leetcode 86. Partition List
最新推荐文章于 2022-10-07 19:26:43 发布