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

293 lines
11 KiB
Java
Raw Normal View History

2021-07-23 15:44:19 +00:00
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
2021-03-25 06:36:36 +00:00
>
>
>
2021-07-23 15:44:19 +00:00
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
2021-03-25 06:36:36 +00:00
2021-07-15 14:47:46 +00:00
2021-03-25 06:24:30 +00:00
[](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)
2021-07-15 16:06:52 +00:00
[](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)思想的话,可以先复习一下,不然这两道题目会看得云里雾里。
2021-03-25 06:24:30 +00:00
#### [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)
2021-07-23 15:44:19 +00:00
O(n)
2021-03-25 06:24:30 +00:00
2021-07-15 14:47:46 +00:00
2021-03-25 06:24:30 +00:00
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325113449.75knzw7zmyg0.png)
2021-07-15 14:47:46 +00:00
绿 5
2021-03-25 06:24:30 +00:00
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325131349.14mi2ap89uxs.png)
绿7 > 554 7
, temphead
```java
while (temphead.next.val <= pre.val) {
2021-07-23 15:44:19 +00:00
temphead = temphead.next;
}
2021-03-25 06:24:30 +00:00
```
2021-07-15 14:47:46 +00:00
****
2021-03-25 06:24:30 +00:00
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/链表的插入排序.4hnc4shp5le0.gif)
2021-07-15 14:47:46 +00:00
2021-03-25 06:24:30 +00:00
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325132359.1hc2axzks3k0.png)
2021-07-15 14:47:46 +00:00
3 2 4 243
2021-03-25 06:24:30 +00:00
2021-07-15 16:06:52 +00:00
4
2021-03-25 06:24:30 +00:00
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/44444444.29mvcvs4yrms.png)
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325134022.w5nnr1spyps.png)
****
2021-04-28 10:28:00 +00:00
Java Code:
2021-03-25 06:24:30 +00:00
```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;
2021-07-23 15:44:19 +00:00
}
2021-03-25 06:24:30 +00:00
//开始出发,查找新元素的合适位置
2021-07-15 14:47:46 +00:00
ListNode temphead = dummyNode;
2021-03-25 06:24:30 +00:00
while (temphead.next.val <= pre.val) {
temphead = temphead.next;
2021-07-23 15:44:19 +00:00
}
2021-03-25 06:24:30 +00:00
//此时我们已经找到了合适位置,我们需要进行插入,大家可以画一画
last.next = pre.next;
pre.next = temphead.next;
temphead.next = pre;
2021-07-15 14:47:46 +00:00
//继续往下移动
2021-03-25 06:24:30 +00:00
pre = last.next;
}
return dummyNode.next;
}
}
```
2021-07-23 15:44:19 +00:00
C++ Code:
2021-04-28 10:28:00 +00:00
```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;
2021-07-23 15:44:19 +00:00
}
2021-04-28 10:28:00 +00:00
//开始出发,查找新元素的合适位置
2021-07-15 16:06:52 +00:00
ListNode * temphead = dummyNode;
2021-04-28 10:28:00 +00:00
while (temphead->next->val <= pre->val) {
2021-07-15 16:06:52 +00:00
temphead = temphead->next;
2021-07-23 15:44:19 +00:00
}
2021-04-28 10:28:00 +00:00
//此时我们已经找到了合适位置,我们需要进行插入,大家可以画一画
last->next = pre->next;
pre->next = temphead->next;
temphead->next = pre;
2021-07-15 14:47:46 +00:00
//继续往下移动
2021-04-28 10:28:00 +00:00
pre = last->next;
}
return dummyNode->next;
}
};
```
2021-03-25 06:24:30 +00:00
2021-07-15 14:47:46 +00:00
JS Code:
```javascript
2021-07-23 15:44:19 +00:00
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;
2021-07-15 14:47:46 +00:00
}
2021-07-23 15:44:19 +00:00
//此时我们已经找到了合适位置,我们需要进行插入,大家可以画一画
last.next = pre.next;
pre.next = tempHead.next;
tempHead.next = pre;
//继续往下移动
pre = last.next;
}
return dummyNode.next;
2021-07-15 14:47:46 +00:00
};
```
Python Code:
2021-07-15 16:06:52 +00:00
```python
2021-07-15 14:47:46 +00:00
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
```
2021-07-17 14:28:06 +00:00
Swift Code
2021-03-25 06:24:30 +00:00
2021-07-17 14:28:06 +00:00
```swift
class Solution {
func insertionSortList(_ head: ListNode?) -> ListNode? {
if head == nil && head?.next == nil {
return head
}
//哑节点
var dummyNode = ListNode(-1)
dummyNode.next = head
//pre负责指向新元素last 负责指向新元素的前一元素
//判断是否需要执行插入操作
var pre = head?.next
var last = head
while pre != nil {
//不需要插入到合适位置,则继续往下移动
if last!.val <= pre!.val {
pre = pre?.next
last = last?.next
continue
}
//开始出发,查找新元素的合适位置
var 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
}
}
```
2021-07-27 18:26:32 +00:00
Go Code:
```go
func insertionSortList(head *ListNode) *ListNode {
if head == nil || head.Next == nil { return head }
root := &ListNode{
Next: head,
}
cur, nxt := head, head.Next
for nxt != nil {
// 有序的不需要换位置
if cur.Val <= nxt.Val {
cur = cur.Next
nxt = nxt.Next
continue
}
temp := root
for temp.Next.Val <= nxt.Val {
temp = temp.Next
}
// 此时找到合适的位置
cur.Next = nxt.Next
nxt.Next = temp.Next
temp.Next = nxt
// 继续向下
nxt = cur.Next
}
return root.Next
}
```