添加py和js,添加注释

pull/33/head
jaredliw 2021-07-15 22:47:46 +08:00
parent 813cfddd0b
commit 4a8c81e88e
1 changed files with 76 additions and 14 deletions

View File

@ -4,7 +4,7 @@
>
> <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)
@ -32,13 +32,11 @@
On,
****
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325113449.75knzw7zmyg0.png)
绿 5
绿 5
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325131349.14mi2ap89uxs.png)
@ -54,13 +52,13 @@
****
****
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/链表的插入排序.4hnc4shp5le0.gif)
@ -68,7 +66,7 @@
3 2 4 243
3 2 4 243
4
@ -87,7 +85,6 @@ Java Code:
```java
class Solution {
public ListNode insertionSortList(ListNode head) {
if (head == null && head.next == null) {
return head;
}
@ -98,7 +95,6 @@ class Solution {
//判断是否需要执行插入操作
ListNode pre = head.next;
ListNode last = head;
ListNode temphead = dummyNode;
while (pre != null) {
//不需要插入到合适位置,则继续往下移动
if (last.val <= pre.val) {
@ -107,7 +103,7 @@ class Solution {
continue;
}
//开始出发,查找新元素的合适位置
temphead = dummyNode;
ListNode temphead = dummyNode;
while (temphead.next.val <= pre.val) {
temphead = temphead.next;
}
@ -115,10 +111,10 @@ class Solution {
last.next = pre.next;
pre.next = temphead.next;
temphead.next = pre;
//继续往下移动
pre = last.next;
}
return dummyNode.next;
}
}
```
@ -139,7 +135,6 @@ public:
//判断是否需要执行插入操作
ListNode * pre = head->next;
ListNode * last = head;
ListNode * temphead = dummyNode;
while (pre != nullptr) {
//不需要插入到合适位置,则继续往下移动
if (last->val <= pre->val) {
@ -150,12 +145,13 @@ public:
//开始出发,查找新元素的合适位置
temphead = dummyNode;
while (temphead->next->val <= pre->val) {
temphead = temphead->next;
ListNode * temphead = temphead->next;
}
//此时我们已经找到了合适位置,我们需要进行插入,大家可以画一画
last->next = pre->next;
pre->next = temphead->next;
temphead->next = pre;
//继续往下移动
pre = last->next;
}
return dummyNode->next;
@ -163,5 +159,71 @@ public:
};
```
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:
```py
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
```