LeetCodeAnimation/0086-Partition-List/Article/0086-Partition-List.md
程序员吴师兄 fb2465e289 整理部分文件
2020-04-16 20:50:43 +08:00

72 lines
2.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# LeetCode 86 号问题分割链表
> 本文首发于公众号图解面试算法 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客https://www.algomooc.com
题目来源于 LeetCode 上第 86 号问题分割链表题目难度为 Easy目前通过率为 47.8%
### 题目描述
给定一个链表和一个特定值 *x*对链表进行分隔使得所有小于 *x* 的节点都在大于或等于 *x* 的节点之前
你应当保留两个分区中每个节点的初始相对位置
**示例:**
```
输入: head = 1->4->3->2->5->2, x = 3
输出: 1->2->2->4->3->5
```
### 题目解析
这道题要求我们划分链表把所有小于给定值的节点都移到前面大于该值的节点顺序不变相当于一个局部排序的问题
- 设定两个虚拟节点`dummyHead1 `用来保存小于于该值的链表`dummyHead2 `来保存大于等于该值的链表
- 遍历整个原始链表将小于该值的放于`dummyHead1 `其余的放置在`dummyHead2 `
- 遍历结束后`dummyHead2 `插入到`dummyHead1 `后面
### 动画描述
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/t96zg.gif)
### 代码实现
```
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* dummyHead1 = new ListNode(-1);
ListNode* dummyHead2 = new ListNode(-1);
ListNode* prev1 = dummyHead1;
ListNode* prev2 = dummyHead2;
for(ListNode* cur = head ; cur != NULL ;){
if(cur->val < x){
prev1->next = cur;
cur = cur->next;
prev1 = prev1->next;
prev1->next = NULL;
}
else{
prev2->next = cur;
cur = cur->next;
prev2 = prev2->next;
prev2->next = NULL;
}
}
prev1->next = dummyHead2->next;
ListNode* ret = dummyHead1->next;
delete dummyHead1;
delete dummyHead2;
return ret;
}
};
```
![](../../Pictures/qrcode.jpg)