Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 505e950

Browse files
committed
add leetcode 147 insertionSort on singly-linkedlist
1 parent 31f8aef commit 505e950

File tree

4 files changed

+174
-5
lines changed

4 files changed

+174
-5
lines changed

linkedlist/Leetcode876MiddleOftheLinkedList.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,8 @@ public ListNode middleNode(ListNode head) {
6767
```
6868
复杂度分析:
6969
- 时间复杂度:_O(N)_
70-
- 空间复杂度:_O(1)_
70+
- 空间复杂度:_O(1)_
71+
72+
73+
74+
冠状病毒

linkedlist/README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class Linkedlist {
5151
Quack.h的内容:
5252
```c
5353
#include<stdio.h>
54-
#include <stdlib.h>
54+
#include<stdlib.h>
5555

5656
typedef struct _node* Quack;
5757

@@ -76,7 +76,7 @@ Quack.c
7676
7777
#define MARKERDATA INT_MAX // dummy data
7878
79-
struct node {
79+
struct _node {
8080
int data;
8181
struct node *next;
8282
};
@@ -233,11 +233,14 @@ gcc Quack.c revarg.c
233233
>> 9876543210
234234
```
235235
236-
237236
[LeetCode876 链表中间节点题解](linkedlist/Leetcode876MiddleOftheLinkedList.md)
238237
239238
[Leetcode19 删除倒数n个节点题解](linkedlist/leetcode19.md)
240239
241240
[Leetcode206 反转链表题解](linkedlist/leetcode206.md)
242241
243-
[Leetcode61 旋转链表题解](linkedlist/leetcode61.md)
242+
[Leetcode61 旋转链表题解](linkedlist/leetcode61.md)
243+
244+
[Leetcode160 相交链表题解](linkedlist/leetcode160.md)
245+
246+
[Leetcode147 链表插入排序题解](linkedlist/leetcode147.md)

linkedlist/leetcode147.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
### 描述
2+
3+
对单链表进行插入排序。
4+
5+
#### 什么是插入排序?
6+
7+
插入排序其实也是一种很简单的排序方法,时间复杂度是O(n^2)。网上介绍这个排序算法的文章有很多,个人推荐去看算法导论的相关章节,讲的非常清楚。个人的理解是,类似我们平时玩扑克牌,手上有一把乱序的牌,每次我们拿一张牌出来,再看我们已经是有序的序列里面,我们找到他的位置,然后进行插入。(这也是它为什么叫插入排序?)
8+
9+
附上原题链接:
10+
11+
[点击此处查看原题](https://leetcode-cn.com/problems/insertion-sort-list/)
12+
13+
### 思路
14+
15+
这题思路比较简单,明白了插入排序我们就好下手了。
16+
17+
这里主要是注意代码的写法。
18+
19+
我们需要一个哨兵节点(dummy node)来保存头结点的信息。
20+
21+
然后我们需要两个辅助节点来帮我们交换节点。话不多说,贴代码:
22+
23+
```java
24+
public ListNode insertionSortList(ListNode head) {
25+
if (head == null || head.next == null) return head;
26+
ListNode dummy = new ListNode(0); // 哨兵节点的使用
27+
dummy.next = head;
28+
ListNode h1;
29+
ListNode h2 = head.next;
30+
head.next = null;
31+
while (h2 != null) {
32+
h1 = dummy;
33+
// find the position.
34+
while (h1.next != null && h1.next.val <= h2.val) {
35+
h1 = h1.next;
36+
}
37+
ListNode nextTemp = h2.next;
38+
h2.next = h1.next;
39+
h1.next = h2;
40+
h2 = nextTemp;
41+
42+
}
43+
return dummy.next;
44+
}
45+
```
46+
47+
大致上就是这样,代码也比较简单,不懂的自己可以画个图来理解,感觉这几天的刷题还是对自己有点帮助的。多刷题就有进步。
48+
49+
> > 愿在追梦路上的人都有收货!
50+

linkedlist/leetcode160.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
### 描述
2+
3+
原题目是找到两个单链表的相交的起始节点。
4+
5+
编写一个程序,找到两个单链表相交的起始节点。
6+
7+
如下面的两个链表:
8+
9+
10+
11+
在节点 c1 开始相交。
12+
13+
14+
15+
示例 1:
16+
17+
18+
19+
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
20+
输出:Reference of the node with value = 8
21+
输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
22+
23+
24+
示例 2:
25+
26+
27+
28+
输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
29+
输出:Reference of the node with value = 2
30+
输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
31+
32+
33+
示例 3:
34+
35+
36+
37+
输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
38+
输出:null
39+
输入解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
40+
解释:这两个链表不相交,因此返回 null。
41+
42+
[点击此处来查看原题](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/)
43+
44+
### 思路
45+
46+
这题有很多思路,比较暴力的就是对于每个节点来进行一次遍历。
47+
48+
时间复杂度:O(mn)
49+
50+
空间复杂度: O(1)
51+
52+
还有一种思路就是哈希表,使用哈希表来存储节点,来找到交点。
53+
54+
时间复杂度:O(m+n)
55+
56+
空间复杂度: O(m+n)
57+
58+
比较好玩的是第三中思路,快慢指针。
59+
60+
附上我的奇葩思路。。。
61+
62+
```java
63+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
64+
int lengthA = 0, lengthB = 0;
65+
ListNode tA = headA;
66+
ListNode tB = headB;
67+
// get the length
68+
while (tA != null) {
69+
tA = tA.next;
70+
lengthA++;
71+
}
72+
while (tB != null) {
73+
tB = tB.next;
74+
lengthB++;
75+
}
76+
if (lengthA >= lengthB) {
77+
// let it move lengthA - lengthB steps.
78+
int step = lengthA - lengthB;
79+
80+
for (int i = 0; i < step; i++) {
81+
headA = headA.next;
82+
}
83+
// then lets go.
84+
for (int i = step; i < lengthA; i++) {
85+
if (headA == headB) {
86+
return headB;
87+
}
88+
headA = headA.next;
89+
headB = headB.next;
90+
}
91+
return null;
92+
}else {
93+
// let it move lengthA - lengthB steps.
94+
int step = lengthB - lengthA;
95+
96+
for (int i = 0; i < step; i++) {
97+
headB = headB.next;
98+
}
99+
// then lets go.
100+
for (int i = step; i < lengthB; i++) {
101+
if (headA == headB) {
102+
return headA;
103+
}
104+
headA = headA.next;
105+
headB = headB.next;
106+
}
107+
return null;
108+
}
109+
}
110+
```
111+
112+
代码有点臭, 但是思路应该很明白了。。。。

0 commit comments

Comments
 (0)