Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b21378

Browse files
添加题目63
1 parent 0dfe0bd commit 1b21378

File tree

6 files changed

+155
-2
lines changed

6 files changed

+155
-2
lines changed

‎60-69/62. Intersection of Two Linked Lists(Easy)/README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
给你两个单链表的头节点 `headA``headB` ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 `null`
66

77
图示两个链表在节点 `c1` 开始相交:
8+
89
![160_statement.png](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/62.%20Intersection%20of%20Two%20Linked%20Lists(Easy)/160_statement.png)
910

1011
题目数据 保证 整个链式结构中不存在环。
@@ -30,6 +31,7 @@
3031
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`.
3132

3233
For example, the following two linked lists begin to intersect at node `c1`:
34+
3335
![160_statement.png](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/62.%20Intersection%20of%20Two%20Linked%20Lists(Easy)/160_statement.png)
3436

3537
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
@@ -55,8 +57,10 @@ The judge will then create the linked structure based on these inputs and pass t
5557

5658

5759
**example 1**
60+
5861
![160_example_1.png](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/62.%20Intersection%20of%20Two%20Linked%20Lists(Easy)/160_example_1.png)
5962

63+
6064
```
6165
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
6266
Output: Intersected at '8'
@@ -67,8 +71,10 @@ From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,
6771

6872

6973
**example 2**
74+
7075
![160_example_2.png](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/62.%20Intersection%20of%20Two%20Linked%20Lists(Easy)/160_example_2.png)
7176

77+
7278
```
7379
Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
7480
Output: Intersected at '2'
@@ -78,8 +84,10 @@ From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,
7884
```
7985

8086
**example 3**
87+
8188
![160_example_3.png](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/62.%20Intersection%20of%20Two%20Linked%20Lists(Easy)/160_example_3.png)
8289

90+
8391
```
8492
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
8593
Output: No intersection
@@ -116,6 +124,7 @@ intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.
116124
1. 解决这个问题的关键是,通过某些方式,让 `p1``p2` 能够同时到达相交节点 `c1`
117125
2. 我们可以让 `p1` 遍历完链表 `A` 之后开始遍历链表 `B`,让 `p2` 遍历完链表 `B` 之后开始遍历链表 `A`,这样相当于「逻辑上」两条链表接在了一起。如果这样进行拼接,就可以让 `p1``p2` 同时进入公共部分,也就是同时到达相交节点 `c1`
118126
3. 具体原理如下图
127+
119128
![img](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/62.%20Intersection%20of%20Two%20Linked%20Lists(Easy)/2.jpeg)
120129

121130

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @lc app=leetcode id=876 lang=cpp
3+
*
4+
* [876] Middle of the Linked List
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* struct ListNode {
11+
* int val;
12+
* ListNode *next;
13+
* ListNode() : val(0), next(nullptr) {}
14+
* ListNode(int x) : val(x), next(nullptr) {}
15+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
16+
* };
17+
*/
18+
class Solution {
19+
public:
20+
ListNode* middleNode(ListNode* head) {
21+
if (head == nullptr)
22+
return head;
23+
ListNode *fast, *slow;
24+
fast = slow = head;
25+
while (fast->next != nullptr && fast->next->next != nullptr)
26+
{
27+
fast = fast->next->next;
28+
slow = slow->next;
29+
}
30+
// 偶数个
31+
if (fast->next != nullptr && fast->next->next == nullptr)
32+
return slow->next;
33+
else
34+
return slow;
35+
}
36+
};
37+
// @lc code=end
38+
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# 链表的中间结点
2+
3+
#### *Middle of the Linked List*
4+
5+
给定一个头结点为 head 的非空单链表,返回链表的中间结点。
6+
7+
如果有两个中间结点,则返回第二个中间结点。
8+
9+
提示:
10+
11+
给定链表的结点数介于 1 和 100 之间。
12+
13+
14+
英文题目:
15+
16+
Given the head of a singly linked list, return the middle node of the linked list.
17+
18+
If there are two middle nodes, return the `second middle` node.
19+
20+
21+
22+
**example 1**
23+
24+
![test 1](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/63.%20Middle%of%the%Linked%List(Easy)/lc-midlist1.jpg)
25+
26+
```
27+
Input: head = [1,2,3,4,5]
28+
Output: [3,4,5]
29+
Explanation: The middle node of the list is node 3.
30+
31+
```
32+
33+
34+
**example 2**
35+
36+
![test 2](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/63.%20Middle%of%the%Linked%List(Easy)/lc-midlist2.jpg)
37+
38+
```
39+
Input: head = [1,2,3,4,5,6]
40+
Output: [4,5,6]
41+
Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.
42+
43+
```
44+
45+
46+
47+
**Constraints:**
48+
49+
The number of nodes in the list is in the range `[1, 100]`.
50+
51+
1 <= `Node.val` <= 100
52+
53+
---
54+
55+
### 思路
56+
57+
1. 使用快慢两个指针,快指针每次走2步,慢指针每次走1步,当快指针到达链表尾部时,慢指针指向链表中点
58+
2. 注意处理链表长度为偶数的情况
59+
60+
61+
### 代码
62+
```
63+
64+
/*
65+
* @lc app=leetcode id=876 lang=cpp
66+
*
67+
* [876] Middle of the Linked List
68+
*/
69+
70+
// @lc code=start
71+
/**
72+
* Definition for singly-linked list.
73+
* struct ListNode {
74+
* int val;
75+
* ListNode *next;
76+
* ListNode() : val(0), next(nullptr) {}
77+
* ListNode(int x) : val(x), next(nullptr) {}
78+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
79+
* };
80+
*/
81+
class Solution {
82+
public:
83+
ListNode* middleNode(ListNode* head) {
84+
if (head == nullptr)
85+
return head;
86+
ListNode *fast, *slow;
87+
fast = slow = head;
88+
while (fast->next != nullptr && fast->next->next != nullptr)
89+
{
90+
fast = fast->next->next;
91+
slow = slow->next;
92+
}
93+
// 偶数个
94+
if (fast->next != nullptr && fast->next->next == nullptr)
95+
return slow->next;
96+
else
97+
return slow;
98+
}
99+
};
100+
// @lc code=end
101+
102+
103+
```
104+
105+
本题以及其它leetcode题目代码github地址: [github地址](https:github.com/SherlockUnknowEn/leetcode)
10.4 KB
Loading[フレーム]
12.2 KB
Loading[フレーム]

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,6 @@
5858
##### 58. [79.Word Search](https://github.com/SherlockUnknowEn/leetcode/tree/master/50-59/58.%20Word%20Search(Medium)) 单词搜索
5959
##### 59. [80.Remove Duplicates from Sorted Array II](https://github.com/SherlockUnknowEn/leetcode/tree/master/50-59/59.%20Remove%20Duplicates%20from%20Sorted%20Array%20II(Medium)) 删除排序数组中的重复项 II
6060
##### 60. [141.Linked List Cycle](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/60.%20Linked%20List%20Cycle(Easy)) 环形链表
61-
##### 61. [142.Linked List Cycle II](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/61.%20Linked%20List%20Cycle%20II(Medium)) 环形链表2
62-
##### 62. [160.Intersection of Two Linked Lists](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/62.%20Intersection%20of%20Two%20Linked%20Lists(Easy)) 环形链表2
61+
##### 61. [142.Linked List Cycle II](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/61.%20Linked%20List%20Cycle%20II(Medium)) 环形链表 II
62+
##### 62. [160.Intersection of Two Linked Lists](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/62.%20Intersection%20of%20Two%20Linked%20Lists(Easy)) 相交链表
63+
##### 63. [876.Middle of the Linked List](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/63.%20Middle%of%the%Linked%List(Easy)) 链表的中间结点

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /