Skip to content

Commit 4a8c81e

Browse files
committed
添加py和js,添加注释
1 parent 813cfdd commit 4a8c81e

File tree

1 file changed

+76
-14
lines changed

1 file changed

+76
-14
lines changed

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

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
>
55
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
66
7-
有的老哥说链表的排序搞不明白,让我写一下,一不小心给忘记了,今天咱们就安排上。没有学过数据结构的同学可以先看下这个文章
7+
有的老哥说链表的排序搞不明白,让我写一下,一不小心给忘记了,今天咱们就安排上。没有学过数据结构的同学可以先看下这个文章
88

99
[【绘图解析】链表详解](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)
1010

@@ -32,13 +32,11 @@
3232

3333
我们知道链表查询元素的时间复杂度为 O(n),我们只能够通过遍历链表查询元素。
3434

35-
那么我们怎么才能将新元素放到合适的位置呢?
36-
37-
**见下图**
35+
那么我们怎么才能将新元素放到合适的位置呢?见下图。
3836

3937
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325113449.75knzw7zmyg0.png)
4038

41-
此时我们不能通过移动绿色指针来寻找 5 的合适位置,那么我们应该怎么做呢?见下图
39+
此时我们不能通过移动绿色指针来寻找 5 的合适位置,那么我们应该怎么做呢?见下图
4240

4341
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325131349.14mi2ap89uxs.png)
4442

@@ -54,21 +52,21 @@
5452

5553
下面我们再来看动画模拟具体过程。
5654

57-
**注:为了更好的表达算法思想,让过程更流畅,特省略了指针的移动细节,直接插入到合适位置,后面会详细说明插入操作的具体步骤**
55+
**注:为了更好的表达算法思想,让过程更流畅,特省略了指针的移动细节,直接插入到合适位置,后面会详细说明插入操作的具体步骤**
5856

5957
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/链表的插入排序.4hnc4shp5le0.gif)
6058

6159
我们通过上面的动画知道了大致过程,那么我们的是如何将新元素插入到指定位置的呢?
6260

63-
见下图
61+
见下图
6462

6563

6664

6765
![插入排序](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325132359.1hc2axzks3k0.png)
6866

6967

7068

71-
我们想要将 3 插入到 2 和 4 的中间,此时我们三个指针分别指向 2,4,3
69+
我们想要将 3 插入到 2 和 4 的中间,此时我们三个指针分别指向 2,4,3
7270

7371
我们共分 4 步,来完成这个操作,见下图
7472

@@ -87,7 +85,6 @@ Java Code:
8785
```java
8886
class Solution {
8987
public ListNode insertionSortList(ListNode head) {
90-
9188
if (head == null && head.next == null) {
9289
return head;
9390
}
@@ -98,7 +95,6 @@ class Solution {
9895
//判断是否需要执行插入操作
9996
ListNode pre = head.next;
10097
ListNode last = head;
101-
ListNode temphead = dummyNode;
10298
while (pre != null) {
10399
//不需要插入到合适位置,则继续往下移动
104100
if (last.val <= pre.val) {
@@ -107,18 +103,18 @@ class Solution {
107103
continue;
108104
}
109105
//开始出发,查找新元素的合适位置
110-
temphead = dummyNode;
106+
ListNode temphead = dummyNode;
111107
while (temphead.next.val <= pre.val) {
112108
temphead = temphead.next;
113109
}
114110
//此时我们已经找到了合适位置,我们需要进行插入,大家可以画一画
115111
last.next = pre.next;
116112
pre.next = temphead.next;
117113
temphead.next = pre;
114+
//继续往下移动
118115
pre = last.next;
119116
}
120117
return dummyNode.next;
121-
122118
}
123119
}
124120
```
@@ -139,7 +135,6 @@ public:
139135
//判断是否需要执行插入操作
140136
ListNode * pre = head->next;
141137
ListNode * last = head;
142-
ListNode * temphead = dummyNode;
143138
while (pre != nullptr) {
144139
//不需要插入到合适位置,则继续往下移动
145140
if (last->val <= pre->val) {
@@ -150,18 +145,85 @@ public:
150145
//开始出发,查找新元素的合适位置
151146
temphead = dummyNode;
152147
while (temphead->next->val <= pre->val) {
153-
temphead = temphead->next;
148+
ListNode * temphead = temphead->next;
154149
}
155150
//此时我们已经找到了合适位置,我们需要进行插入,大家可以画一画
156151
last->next = pre->next;
157152
pre->next = temphead->next;
158153
temphead->next = pre;
154+
//继续往下移动
159155
pre = last->next;
160156
}
161157
return dummyNode->next;
162158
}
163159
};
164160
```
165161
162+
JS Code:
163+
164+
```javascript
165+
var insertionSortList = function(head) {
166+
if (head === null || head.next === null) return head;
167+
//哑节点
168+
let dummyNode = new ListNode(-1, head);
169+
let pre = head.next;
170+
//pre负责指向新元素,last 负责指向新元素的前一元素
171+
//判断是否需要执行插入操作
172+
let last = head;
173+
while (pre) {
174+
//不需要插入到合适位置,则继续往下移动
175+
if (last.val <= pre.val) {
176+
last = last.next;
177+
pre = pre.next;
178+
continue;
179+
}
180+
//开始出发,查找新元素的合适位置
181+
let tempHead = dummyNode;
182+
while (tempHead.next.val <= pre.val) {
183+
tempHead = tempHead.next;
184+
}
185+
//此时我们已经找到了合适位置,我们需要进行插入,大家可以画一画
186+
last.next = pre.next;
187+
pre.next = tempHead.next;
188+
tempHead.next = pre;
189+
//继续往下移动
190+
pre = last.next;
191+
}
192+
return dummyNode.next;
193+
};
194+
```
195+
196+
Python Code:
197+
198+
```py
199+
class Solution:
200+
def insertionSortList(self, head: ListNode) -> ListNode:
201+
if head is None or head.next is None:
202+
return head
203+
# 哑节点
204+
dummyNode = ListNode(-1, head)
205+
# pre负责指向新元素,last 负责指向新元素的前一元素
206+
# 判断是否需要执行插入操作
207+
pre = head.next
208+
last = head
209+
while pre is not None:
210+
# 不需要插入到合适位置,则继续往下移动
211+
if last.val <= pre.val:
212+
pre = pre.next
213+
last = last.next
214+
continue
215+
# 开始出发,查找新元素的合适位置
216+
temphead = dummyNode
217+
while temphead.next.val <= pre.val:
218+
temphead = temphead.next
219+
# 此时我们已经找到了合适位置,我们需要进行插入,大家可以画一画
220+
last.next = pre.next
221+
pre.next = temphead.next
222+
temphead.next = pre
223+
# 继续往下移动
224+
pre = last.next
225+
return dummyNode.next
226+
```
227+
166228

167229

0 commit comments

Comments
 (0)