Skip to content

Commit 82975ee

Browse files
committed
inertion-sort-list
1 parent 3603b49 commit 82975ee

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# insertion-sort-list(使用插入排序对链表进行排序)
2+
3+
<center>知识点:</center>
4+
5+
6+
## 题目描述
7+
8+
Sort a linked list using insertion sort.
9+
10+
## 解题思路
11+
12+
首先明确什么是数组的插入排序:
13+
首先将第二个数与第一个数进行对比,如果第二个数比第一个数小,则将第二个数插入到第一个数之前,这样保证前两个数是有序的;
14+
接下来将第三个数与前两个数对比,发现有比第三个数大的数即将第三个数插入到对应数的前面,这样一次插入可保证前三个数是有序的;
15+
以此类推,将后面的i个数分别其前面的i-1个数进行对比,并将其插入到第一个比其大的数前面,最后即可完成排序。
16+
17+
链表与数组相比唯一的不同在于链表不能通过下标访问元素,而且链表不能像数组那样通过下标减1的方式访问到上一个元素,针对这种情况我们将上面的算法做出如下改进:
18+
19+
​ 使用curNode、nextNode两个指针,curNode初始指向head,nextNode初始指向curNode.next,curNode的意思是从head节点开始到curNode节点之间的所有元素均为有序的,这在初始时时成立的(初始时curNode=head,所以当然成立),nextNode指向的是curNode的下一个元素,我们使用这个nextNode指针的目的是判断已经确定的有序链表[head,curNode]之外的下一个节点与当前有序链表的关系,具体的:
20+
21+
- 如果nextNode.val>=curNode.val,那么说明[head,curNode]+nextNode构成的链表也是有序的,所以应该讲curNode向后移,即curNode=curNode.next,同时nextNode=curNode.next.
22+
- 如果nextNode.val<curNode.val,这个时候我们就需要将nextNode这个节点插入到之前有序链表的某一个位置,以保证[head,curNode]+headNode构成的链表是有序的,那么插在那个位置呢?就需要从head节点开始往后找了,方法是找到第一个比nextNode.val大的节点记为P,然后将nextNode插入到P之前即可,需要注意的是链表插入的时候需要借助一个辅助指针记住插入位置的前一个节点以方便插入。
23+
- 如果nextNode==null,说明整个链表排序完成,返回即可。
24+
25+
26+
27+
28+
## 代码
29+
30+
[这里](../src/five/Solution.java)
Binary file not shown.
Binary file not shown.

LeetCode/src/five/Solution.java

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package five;
2+
3+
/**
4+
* @author dmrfcoder
5+
* @date 2019/4/10
6+
*/
7+
8+
class ListNode {
9+
int val;
10+
ListNode next;
11+
12+
ListNode(int x) {
13+
val = x;
14+
next = null;
15+
}
16+
}
17+
18+
public class Solution {
19+
public ListNode insertionSortList(ListNode head) {
20+
if (head == null || head.next == null) {
21+
return head;
22+
}
23+
24+
ListNode tempHead = new ListNode(0);
25+
tempHead.next = head;
26+
27+
28+
ListNode curNode = head;
29+
ListNode indexNode;
30+
31+
while (curNode != null) {
32+
33+
indexNode = curNode.next;
34+
while (indexNode != null && indexNode.val >= curNode.val) {
35+
indexNode = indexNode.next;
36+
curNode = curNode.next;
37+
}
38+
39+
if (indexNode == null) {
40+
return tempHead.next;
41+
}
42+
43+
ListNode copyOfTempHead = tempHead;
44+
ListNode copyOfHead = tempHead.next;
45+
46+
while (copyOfHead.val < indexNode.val) {
47+
copyOfTempHead = copyOfTempHead.next;
48+
copyOfHead = copyOfHead.next;
49+
}
50+
copyOfTempHead.next = indexNode;
51+
curNode.next = indexNode.next;
52+
indexNode.next = copyOfHead;
53+
54+
55+
}
56+
57+
return tempHead.next;
58+
}
59+
60+
public static void main(String[] args) {
61+
ListNode head = new ListNode(3);
62+
ListNode node2 = new ListNode(2);
63+
ListNode node1 = new ListNode(1);
64+
head.next = node2;
65+
node2.next = node1;
66+
67+
Solution solution = new Solution();
68+
solution.insertionSortList(head);
69+
}
70+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,5 @@
146146
- [evaluate-reverse-polish-notation(计算逆波兰式的值)](./LeetCode/Doc/计算逆波兰式的值.md)
147147
- [max-points-on-a-line(同一条直线上的最多的点的数量)](./LeetCode/Doc/同一条直线上的最多的点的数量.md)
148148
- [sort-list(排序List)](./LeetCode/Doc/排序List.md)
149+
- [insertion-sort-list(使用插入排序对链表进行排序)](./LeetCode/Doc/使用插入排序对链表进行排序.md)
149150

0 commit comments

Comments
 (0)