4
4
>
5
5
> 另外希望手机阅读的同学可以来我的 <u >[ ** 公众号:袁厨的算法小屋** ] ( https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png ) </u > 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u >[ ** 刷题小队** ] ( https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png ) </u >进入。
6
6
7
- 有的老哥说链表的排序搞不明白,让我写一下,一不小心给忘记了,今天咱们就安排上。没有学过数据结构的同学可以先看下这个文章
7
+ 有的老哥说链表的排序搞不明白,让我写一下,一不小心给忘记了,今天咱们就安排上。没有学过数据结构的同学可以先看下这个文章:
8
8
9
9
[ 【绘图解析】链表详解] ( 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 )
10
10
32
32
33
33
我们知道链表查询元素的时间复杂度为 O(n),我们只能够通过遍历链表查询元素。
34
34
35
- 那么我们怎么才能将新元素放到合适的位置呢?
36
-
37
- ** 见下图**
35
+ 那么我们怎么才能将新元素放到合适的位置呢?见下图。
38
36
39
37
![ ] ( https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325113449.75knzw7zmyg0.png )
40
38
41
- 此时我们不能通过移动绿色指针来寻找 5 的合适位置,那么我们应该怎么做呢?见下图
39
+ 此时我们不能通过移动绿色指针来寻找 5 的合适位置,那么我们应该怎么做呢?见下图。
42
40
43
41
![ ] ( https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325131349.14mi2ap89uxs.png )
44
42
54
52
55
53
下面我们再来看动画模拟具体过程。
56
54
57
- ** 注:为了更好的表达算法思想,让过程更流畅,特省略了指针的移动细节,直接插入到合适位置,后面会详细说明插入操作的具体步骤**
55
+ ** 注:为了更好的表达算法思想,让过程更流畅,特省略了指针的移动细节,直接插入到合适位置,后面会详细说明插入操作的具体步骤。 **
58
56
59
57
![ ] ( https://cdn.jsdelivr.net/gh/tan45du/photobed@master/链表的插入排序.4hnc4shp5le0.gif )
60
58
61
59
我们通过上面的动画知道了大致过程,那么我们的是如何将新元素插入到指定位置的呢?
62
60
63
- 见下图
61
+ 见下图。
64
62
65
63
66
64
67
65
![ 插入排序] ( https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325132359.1hc2axzks3k0.png )
68
66
69
67
70
68
71
- 我们想要将 3 插入到 2 和 4 的中间,此时我们三个指针分别指向 2,4,3
69
+ 我们想要将 3 插入到 2 和 4 的中间,此时我们三个指针分别指向 2,4,3。
72
70
73
71
我们共分 4 步,来完成这个操作,见下图
74
72
@@ -87,7 +85,6 @@ Java Code:
87
85
``` java
88
86
class Solution {
89
87
public ListNode insertionSortList (ListNode head ) {
90
-
91
88
if (head == null && head. next == null ) {
92
89
return head;
93
90
}
@@ -98,7 +95,6 @@ class Solution {
98
95
// 判断是否需要执行插入操作
99
96
ListNode pre = head. next;
100
97
ListNode last = head;
101
- ListNode temphead = dummyNode;
102
98
while (pre != null ) {
103
99
// 不需要插入到合适位置,则继续往下移动
104
100
if (last. val <= pre. val) {
@@ -107,18 +103,18 @@ class Solution {
107
103
continue ;
108
104
}
109
105
// 开始出发,查找新元素的合适位置
110
- temphead = dummyNode;
106
+ ListNode temphead = dummyNode;
111
107
while (temphead. next. val <= pre. val) {
112
108
temphead = temphead. next;
113
109
}
114
110
// 此时我们已经找到了合适位置,我们需要进行插入,大家可以画一画
115
111
last. next = pre. next;
116
112
pre. next = temphead. next;
117
113
temphead. next = pre;
114
+ // 继续往下移动
118
115
pre = last. next;
119
116
}
120
117
return dummyNode. next;
121
-
122
118
}
123
119
}
124
120
```
@@ -139,7 +135,6 @@ public:
139
135
//判断是否需要执行插入操作
140
136
ListNode * pre = head->next;
141
137
ListNode * last = head;
142
- ListNode * temphead = dummyNode;
143
138
while (pre != nullptr) {
144
139
//不需要插入到合适位置,则继续往下移动
145
140
if (last->val <= pre->val) {
@@ -150,18 +145,85 @@ public:
150
145
//开始出发,查找新元素的合适位置
151
146
temphead = dummyNode;
152
147
while (temphead->next->val <= pre->val) {
153
- temphead = temphead->next;
148
+ ListNode * temphead = temphead->next;
154
149
}
155
150
//此时我们已经找到了合适位置,我们需要进行插入,大家可以画一画
156
151
last->next = pre->next;
157
152
pre->next = temphead->next;
158
153
temphead->next = pre;
154
+ //继续往下移动
159
155
pre = last->next;
160
156
}
161
157
return dummyNode->next;
162
158
}
163
159
};
164
160
```
165
161
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
+
166
228
167
229
0 commit comments