Skip to content

Commit a767083

Browse files
committedNov 26, 2023
2023/11/26
1 parent 8abe591 commit a767083

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed
 

‎鏈表/142.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 142. Linked List Cycle II(環形鍊表)
2+
3+
[參考影片解析](https://www.bilibili.com/video/BV1if4y1d7ob/)
4+
5+
[參考連結](https://github.com/youngyangyang04/leetcode-master/blob/master/problems/0142.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8II.md)
6+
7+
**題目**
8+
9+
Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.
10+
11+
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter.
12+
13+
Do not modify the linked list.
14+
15+
**ex1**
16+
17+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png)
18+
19+
Input: head = [3,2,0,-4], pos = 1
20+
Output: tail connects to node index 1
21+
Explanation: There is a cycle in the linked list, where tail connects to the second node.
22+
23+
**ex2**
24+
25+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png)
26+
27+
Input: head = [1,2], pos = 0
28+
Output: tail connects to node index 0
29+
Explanation: There is a cycle in the linked list, where tail connects to the first node.
30+
31+
**ex3**
32+
33+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png)
34+
35+
Input: head = [1], pos = -1
36+
Output: no cycle
37+
Explanation: There is no cycle in the linked list.
38+
39+
Constraints:
40+
41+
- The number of the nodes in the list is in the range [0, 104].
42+
- -10^5 <= Node.val <= 10^5
43+
- pos is -1 or a valid index in the linked-list.
44+
45+
**程式碼**
46+
47+
struct ListNode *detectCycle(struct ListNode *head) {
48+
typedef struct ListNode ListNode;
49+
ListNode *fastcur = head;
50+
ListNode *lowcur = head;
51+
while(fastcur!=NULL && fastcur->next!=NULL){
52+
fastcur = fastcur->next->next;
53+
lowcur = lowcur->next;
54+
if (fastcur==lowcur){
55+
ListNode *index1 = fastcur;
56+
ListNode *index2 = head;
57+
while(index1!=index2){
58+
index1 = index1->next;
59+
index2 = index2->next;
60+
}
61+
return index1;
62+
}
63+
}
64+
return NULL;
65+
}
66+
67+
**備註**
68+
- 利用雙指針法
69+
- 若快慢指針相遇,判斷index1是否等於index2,index1從快慢指針相遇點出發,index2從頭節點出發,兩者相遇的地方就會是環形的入口。
70+
- 若快慢指針沒有相遇則返回NULL,即該鍊表非環形鍊表。

‎鏈表/160.md

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# 160. Intersection of Two Linked Lists (鍊表相交)
2+
3+
[參考來源](https://github.com/youngyangyang04/leetcode-master/blob/master/problems/%E9%9D%A2%E8%AF%95%E9%A2%9802.07.%E9%93%BE%E8%A1%A8%E7%9B%B8%E4%BA%A4.md)
4+
5+
**題目**
6+
7+
Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.
8+
9+
For example, the following two linked lists begin to intersect at node c1
10+
11+
![](https://assets.leetcode.com/uploads/2021/03/05/160_statement.png)
12+
13+
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
14+
15+
Note that the linked lists must retain their original structure after the function returns.
16+
17+
Custom Judge:
18+
19+
The inputs to the judge are given as follows (your program is not given these inputs):
20+
21+
- intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node.
22+
- listA - The first linked list.
23+
- listB - The second linked list.
24+
- skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.
25+
- skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.
26+
27+
The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.
28+
29+
30+
**ex1**
31+
32+
![](https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png)
33+
34+
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
35+
Output: Intersected at '8'
36+
Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
37+
From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
38+
- Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (3rd node in A and 4th node in B) point to the same location in memory.
39+
40+
**ex2**
41+
42+
![](https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png)
43+
44+
Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
45+
Output: Intersected at '2'
46+
Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
47+
From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
48+
49+
**ex3**
50+
51+
![](https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png)
52+
53+
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
54+
Output: No intersection
55+
Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
56+
Explanation: The two lists do not intersect, so return null.
57+
58+
Constraints:\
59+
- The number of nodes of listA is in the m.
60+
- The number of nodes of listB is in the n.
61+
- 1 <= m, n <= 3 * 104
62+
- 1 <= Node.val <= 105
63+
- 0 <= skipA < m
64+
- 0 <= skipB < n
65+
- intersectVal is 0 if listA and listB do not intersect.
66+
- intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.
67+
68+
## 解題思路
69+
70+
- 題目意在求出兩個鍊表交點節點的指針,而不是數值!
71+
- step1: 找出兩個鍊表的長度
72+
- step2: 將常鍊表的指針與短鍊表對齊
73+
- step3: 同時移動並比較下一個節點是否相同
74+
- step4: 相同則返回鍊表,若不相同則返回NULL
75+
76+
**程式碼**
77+
78+
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
79+
typedef struct ListNode ListNode;
80+
ListNode *curA = headA;
81+
ListNode *curB = headB;
82+
int lenA=0, lenB=0;
83+
//計算鍊表長度
84+
while(curA != NULL){
85+
lenA++;
86+
curA = curA->next;
87+
}
88+
while(curB != NULL){
89+
lenB++;
90+
curB = curB->next;
91+
}
92+
93+
curA = headA;
94+
curB = headB;
95+
int gap;
96+
// 讓lenA為最長的鍊表,比較長度、計算差值
97+
if (lenA > lenB){
98+
gap = lenA - lenB;
99+
}
100+
else{
101+
ListNode *temp = curA;
102+
curA = curB;
103+
curB = temp;
104+
gap = lenB - lenA;
105+
}
106+
//移動cur
107+
while(gap--){
108+
curA = curA->next;
109+
}
110+
//遍歷鍊表,遇到相同則返回,遇到不同則返回NULL
111+
while(curA != NULL){
112+
if (curA == curB){
113+
return curA;
114+
}
115+
curA = curA->next;
116+
curB = curB->next;
117+
}
118+
return NULL;
119+
}

‎鏈表/203.md

+3
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,6 @@ Given the head of a linked list and an integer val, remove all the nodes of the
9393
+ typedef struct ListNode ListNode;目的在於是創建一個新的型別別名,以簡化對 struct ListNode 的使用。
9494
+ ListNode *dummyHead;等價於struct ListNode *dummyHead;
9595
+ 在C裡面必需對虛擬頭節點進行動態記憶體分配:dummyHead = (ListNode *)malloc(sizeof(ListNode));。也就是說,如果不進行記憶體的分配,後續的dummyHead將無法僅行使用。
96+
97+
98+

‎鏈表/conclusion.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 總結
2+
3+
[參考來源](https://github.com/youngyangyang04/leetcode-master/blob/master/problems/%E9%93%BE%E8%A1%A8%E6%80%BB%E7%BB%93%E7%AF%87.md)
4+
5+
6+
![](https://camo.githubusercontent.com/fb8a648b568e8169f50670d0d0dcdd70867bd3b3f155776bd16d6480ab361873/68747470733a2f2f636f64652d7468696e6b696e672d313235333835353039332e66696c652e6d7971636c6f75642e636f6d2f706963732f2545392539332542452545382541312541382545362538302542422545372542422539332e706e67)
7+
8+
圖片來源: 代码随想录知识星球-海螺人

0 commit comments

Comments
 (0)
Please sign in to comment.