algorithm-base/animation-simulation/链表篇/leetcode86分隔链表.md

160 lines
4.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [86. ](https://leetcode-cn.com/problems/partition-list/)
head x 使 x x
![](https://img-blog.csdnimg.cn/20210319190335143.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70)
1
head = [1,4,3,2,5,2], x = 3
[1,2,2,4,3,5]
2
head = [2,1], x = 2
[1,2]
LeetCode
x >= big small big null
![](https://img-blog.csdnimg.cn/20210319190417499.gif)
****
Java Code:
```java
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode pro = head;
ListNode big = new ListNode(-1);
ListNode small = new ListNode(-1);
ListNode headbig = big;
ListNode headsmall = small;
//分
while (pro != null) {
//大于时,放到 big 链表上
if (pro.val >= x) {
big.next = pro;
big = big.next;
//小于时,放到 small 链表上
}else {
small.next = pro;
small = small.next;
}
pro = pro.next;
}
//细节
big.next = null;
//合
small.next = headbig.next;
return headsmall.next;
}
}
```
C++ Code:
```cpp
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode * pro = head;
ListNode * big = new ListNode(-1);
ListNode * small = new ListNode(-1);
ListNode * headbig = big;
ListNode * headsmall = small;
//分
while (pro != nullptr) {
//大于时,放到 big 链表上
if (pro->val >= x) {
big->next = pro;
big = big->next;
//小于时,放到 small 链表上
}else {
small->next = pro;
small = small->next;
}
pro = pro->next;
}
//细节
big->next = nullptr;
//合
small->next = headbig->next;
return headsmall->next;
}
};
```
JS Code:
```js
var partition = function(head, x) {
let pro = head;
let big = new ListNode(-1);
let small = new ListNode(-1);
let headbig = big;
let headsmall = small;
//分
while (pro) {
//大于时,放到 big 链表上
if (pro.val >= x) {
big.next = pro;
big = big.next;
//小于时,放到 small 链表上
}else {
small.next = pro;
small = small.next;
}
pro = pro.next;
}
//细节
big.next = null;
//合
small.next = headbig.next;
return headsmall.next;
};
```
Python Code:
```python
class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
pro = head
big = ListNode(-1)
small = ListNode(-1)
headbig = big
headsmall = small
#
while pro is not None:
# big
if pro.val >= x:
big.next = pro
big = big.next
# small
else:
small.next = pro
small = small.next
pro = pro.next
#
big.next = None
#
small.next = headbig.next
return headsmall.next
```