algorithm-base/animation-simulation/链表篇/leetcode147对链表进行插入排序.md

230 lines
9.0 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>进入。
[](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/%E5%85%B3%E4%BA%8E%E9%93%BE%E8%A1%A8%E7%9A%84%E9%82%A3%E4%BA%9B%E4%BA%8B.md)
[](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/%E7%9B%B4%E6%8E%A5%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F.md)和[【动画模拟】归并排序](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F.md)思想的话,可以先复习一下,不然这两道题目会看得云里雾里。
#### [147. ](https://leetcode-cn.com/problems/insertion-sort-list/)
** 1**
> : 4->2->1->3
> : 1->2->3->4
** 2**
> : -1->5->3->4->0
> : -1->0->3->4->5
![](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/直接插入排序.2marc4epuzy0.gif)
O(n)
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325113449.75knzw7zmyg0.png)
绿 5
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325131349.14mi2ap89uxs.png)
绿7 > 554 7
, temphead
```java
while (temphead.next.val <= pre.val) {
temphead = temphead.next;
}
```
****
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/链表的插入排序.4hnc4shp5le0.gif)
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325132359.1hc2axzks3k0.png)
3 2 4 243
4
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/44444444.29mvcvs4yrms.png)
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325134022.w5nnr1spyps.png)
****
Java Code:
```java
class Solution {
public ListNode insertionSortList(ListNode head) {
if (head == null && head.next == null) {
return head;
}
//哑节点
ListNode dummyNode = new ListNode(-1);
dummyNode.next = head;
//pre负责指向新元素last 负责指向新元素的前一元素
//判断是否需要执行插入操作
ListNode pre = head.next;
ListNode last = head;
while (pre != null) {
//不需要插入到合适位置,则继续往下移动
if (last.val <= pre.val) {
pre = pre.next;
last = last.next;
continue;
}
//开始出发,查找新元素的合适位置
ListNode temphead = dummyNode;
while (temphead.next.val <= pre.val) {
temphead = temphead.next;
}
//此时我们已经找到了合适位置,我们需要进行插入,大家可以画一画
last.next = pre.next;
pre.next = temphead.next;
temphead.next = pre;
//继续往下移动
pre = last.next;
}
return dummyNode.next;
}
}
```
C++ Code:
```cpp
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if (head == nullptr && head->next == nullptr) {
return head;
}
//哑节点
ListNode * dummyNode = new ListNode(-1);
dummyNode->next = head;
//pre负责指向新元素last 负责指向新元素的前一元素
//判断是否需要执行插入操作
ListNode * pre = head->next;
ListNode * last = head;
while (pre != nullptr) {
//不需要插入到合适位置,则继续往下移动
if (last->val <= pre->val) {
pre = pre->next;
last = last->next;
continue;
}
//开始出发,查找新元素的合适位置
ListNode * temphead = dummyNode;
while (temphead->next->val <= pre->val) {
temphead = temphead->next;
}
//此时我们已经找到了合适位置,我们需要进行插入,大家可以画一画
last->next = pre->next;
pre->next = temphead->next;
temphead->next = pre;
//继续往下移动
pre = last->next;
}
return dummyNode->next;
}
};
```
JS Code:
```javascript
var insertionSortList = function(head) {
if (head === null || head.next === null) return head;
//哑节点
let dummyNode = new ListNode(-1, head);
let pre = head.next;
//pre负责指向新元素last 负责指向新元素的前一元素
//判断是否需要执行插入操作
let last = head;
while (pre) {
//不需要插入到合适位置,则继续往下移动
if (last.val <= pre.val) {
last = last.next;
pre = pre.next;
continue;
}
//开始出发,查找新元素的合适位置
let tempHead = dummyNode;
while (tempHead.next.val <= pre.val) {
tempHead = tempHead.next;
}
//此时我们已经找到了合适位置,我们需要进行插入,大家可以画一画
last.next = pre.next;
pre.next = tempHead.next;
tempHead.next = pre;
//继续往下移动
pre = last.next;
}
return dummyNode.next;
};
```
Python Code:
```python
class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
if head is None or head.next is None:
return head
#
dummyNode = ListNode(-1, head)
# prelast
#
pre = head.next
last = head
while pre is not None:
#
if last.val <= pre.val:
pre = pre.next
last = last.next
continue
#
temphead = dummyNode
while temphead.next.val <= pre.val:
temphead = temphead.next
#
last.next = pre.next
pre.next = temphead.next
temphead.next = pre
#
pre = last.next
return dummyNode.next
```