LeetCode刷题笔记(链表):partition-list



[toc]

题目描述

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

For example,
Given1->4->3->2->5->2and x = 3,
return1->2->2->4->3->5.

解题思路

牛客网上面的答案都是新建两个链表,小于x的放到一个链表里面,不小于的放到另一个链表里面,这种答案感觉好没劲哦。所以最后我采用的是O(n)时间复杂度,O(1)空间复杂度的解法。

具体说来,还是用快慢指针遍历链表,slow指向连续小于x的最后一个元素,fast指向当前元素不小于x但是下个元素小于x的元素。理解清楚这两个指针的对应关系之后,我们很容易将fast指向的元素的下一个元素追加到slow之后,同时更新slow和fast的指。

C++版代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 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 *dummy = new ListNode(0);
dummy->next = head;
ListNode *fast = dummy;
ListNode *slow = dummy;
while(fast != NULL && fast->next != NULL){
if(fast->next->val >= x)
fast = fast->next;
else{
if(fast == slow){
fast = fast->next;
slow = slow->next;
}
else{
ListNode *tmp = fast->next;
fast->next = tmp->next;
tmp->next = slow->next;
slow->next = tmp;
slow = slow->next;

}
}
}
return dummy->next;
}
};

系列教程持续发布中,欢迎订阅、关注、收藏、评论、点赞哦~~( ̄▽ ̄~)~

完的汪(∪。∪)。。。zzz

0%