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

[pull] main from itcharge:main #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 7 commits into AlgorithmAndLeetCode:main from itcharge:main
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions Solutions/0023. 合并K个升序链表.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,50 @@

## 题目大意

给定一个链表数组,每个链表都已经按照升序排列。
**描述**:给定一个链表数组,每个链表都已经按照升序排列。

要求:将所有链表合并到一个升序链表中,返回合并后的链表。
**要求**:将所有链表合并到一个升序链表中,返回合并后的链表。

**说明**:

- $k == lists.length$。
- 0ドル \le k \le 10^4$。
- 0ドル \le lists[i].length \le 500$。
- $-10^4 \le lists[i][j] \le 10^4$。
- $lists[i]$ 按升序排列。
- $lists[i].length$ 的总和不超过 10ドル^4$。

**示例**:

- 示例 1:

```Python
输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[
1->4->5,
1->3->4,
2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6
```

- 示例 2:

```Python
输入:lists = []
输出:[]
```

## 解题思路

### 思路 1:分治

分而治之的思想。将链表数组不断二分,转为规模为二分之一的子问题,然后再进行归并排序。

##代码
### 思路 1:代码

```Python
class Solution:
Expand Down Expand Up @@ -49,3 +84,8 @@ class Solution:
return self.merge_sort(lists, 0, size - 1)
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(k \times n \times \log_2k)$。
- **空间复杂度**:$O(\log_2k)$。

7 changes: 6 additions & 1 deletion Solutions/0025. K 个一组翻转链表.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
6. 最后等到 `cur` 遍历到链表末尾(即 `cur == tail`)时,令「当前待反转区间的第一个节点的前一个节点」指向「反转区间后的头节点」 ,即 `head.next = pre`。令「待反转区间的第一个节点(反转之后为区间的尾节点)」指向「待反转分区间的最后一个节点的后一个节点」,即 `first.next = tail`。
7. 最后返回新的头节点 `dummy_head.next`。

### 思路 1:迭代代码
### 思路 1:代码

```Python
# Definition for singly-linked list.
Expand Down Expand Up @@ -96,3 +96,8 @@ class Solution:
tail = tail.next
return dummy_head.next
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(n)$。其中 $n$ 为链表的总长度。
- **空间复杂度**:$O(1)$。
7 changes: 6 additions & 1 deletion Solutions/0082. 删除排序链表中的重复元素 II.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
- 如果下一个元素值和下下一个元素值不同,则令 `cur` 向右移动一位,继续向后遍历。
- 当指针 `cur` 的下一个元素或者下下一个元素不存在时,说明已经遍历完,则返回哑节点 `dummy_head` 的下一个节点作为头节点。

### 思路 1:遍历代码
### 思路 1:代码

```Python
# Definition for singly-linked list.
Expand All @@ -61,3 +61,8 @@ class Solution:
cur = cur.next
return dummy_head.next
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(n)$。其中 $n$ 为链表长度。
- **空间复杂度**:$O(1)$。
4 changes: 4 additions & 0 deletions Solutions/0083. 删除排序链表中的重复元素.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ class Solution:
return head
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(n)$。其中 $n$ 为链表长度。
- **空间复杂度**:$O(1)$。
5 changes: 5 additions & 0 deletions Solutions/0092. 反转链表 II.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class Solution:
return dummy_head.next
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(n)$。其中 $n$ 是链表节点个数。
- **空间复杂度**:$O(1)$。

## 参考资料

- 【题解】[动画图解:翻转链表的指定区间 - 反转链表 II - 力扣](https://leetcode.cn/problems/reverse-linked-list-ii/solution/dong-hua-tu-jie-fan-zhuan-lian-biao-de-z-n4px/)
Expand Down
39 changes: 34 additions & 5 deletions Solutions/0143. 重排链表.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,41 @@

## 题目大意

给定一个单链表 `L` 的头节点 `head`,单链表 `L` 表示为:$L_0$ -> $L_1$ -> $L_2$ -> ... -> $L_{n-1}$ -> $L_n$。
**描述**:给定一个单链表 `L` 的头节点 `head`,单链表 `L` 表示为:$L_0$ -> $L_1$ -> $L_2$ -> ... -> $L_{n-1}$ -> $L_n$。

要求:将单链表 `L` 重新排列为:$L_0$ -> $L_n$ -> $L_1$ -> $L_{n-1}$ -> $L_2$ -> $L_{n-2}$ -> $L_3$ -> $L_{n-3}$ -> ...。
**要求**:将单链表 `L` 重新排列为:$L_0$ -> $L_n$ -> $L_1$ -> $L_{n-1}$ -> $L_2$ -> $L_{n-2}$ -> $L_3$ -> $L_{n-3}$ -> ...。

注意:需要将实际节点进行交换。
**说明**:

- 需要将实际节点进行交换。

**示例**:

- 示例 1:

![](https://pic.leetcode-cn.com/1626420311-PkUiGI-image.png)

```Python
输入:head = [1,2,3,4]
输出:[1,4,2,3]
```

- 示例 2:

![](https://pic.leetcode-cn.com/1626420320-YUiulT-image.png)

```Python
输入:head = [1,2,3,4,5]
输出:[1,5,2,4,3]
```

## 解题思路

链表不能像数组那样直接进行随机访问。所以我们可以先将链表转为线性表。然后直接按照提要要求的排列顺序访问对应数据元素,重新建立链表。
### 思路 1:线性表

## 代码
因为链表无法像数组那样直接进行随机访问。所以我们可以先将链表转为线性表,然后直接按照提要要求的排列顺序访问对应数据元素,重新建立链表。

### 思路 1:代码

```Python
class Solution:
Expand Down Expand Up @@ -43,3 +67,8 @@ class Solution:
vec[left].next = None
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(n)$。
- **空间复杂度**:$O(n)$。

61 changes: 50 additions & 11 deletions Solutions/0160. 相交链表.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,67 @@

## 题目大意

给定 A、B 两个链表,判断两个链表是否相交,返回相交的起始点。如果不相交,则返回 None
**描述**:给定 `listA`、`listB` 两个链表。

比如:链表 A 为 [4, 1, 8, 4, 5],链表 B 为 [5, 0, 1, 8, 4, 5]。则如下图所示,两个链表相交的起始节点为 8,则输出结果为 8。
**要求**:判断两个链表是否相交,返回相交的起始点。如果不相交,则返回 `None`。

**说明**:

- `listA` 中节点数目为 $m$。
- `listB` 中节点数目为 $n$。
- 1ドル \le m, n \le 3 * 10^4$。
- 1ドル \le Node.val \le 10^5$。
- 0ドル \le skipA \le m$。
- 0ドル \le skipB \le n$。
- 如果 `listA` 和 `listB` 没有交点,`intersectVal` 为 0ドル$。
- 如果 `listA` 和 `listB` 有交点,`intersectVal == listA[skipA] == listB[skipB]`。

**示例**:

- 示例 1:

![](https://assets.leetcode.com/uploads/2018/12/13/160_example_1.png)

```Python
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
输出:Intersected at '8'
解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,6,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
— 请注意相交节点的值不为 1,因为在链表 A 和链表 B 之中值为 1 的节点 (A 中第二个节点和 B 中第三个节点) 是不同的节点。换句话说,它们在内存中指向两个不同的位置,而链表 A 和链表 B 中值为 8 的节点 (A 中第三个节点,B 中第四个节点) 在内存中指向相同的位置。
```

- 示例 2:

![](https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png)

```Python
输入:intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Intersected at '2'
解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [1,9,1,2,4],链表 B 为 [3,2,4]。
在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
```

## 解题思路

如果两个链表相交,那么从相交位置开始,到结束,必有一段等长且相同的节点。假设链表 A 的长度为 m、链表 B 的长度为 n,他们的相交序列有 k 个,则相交情况可以如下如所示:
### 思路 1:双指针

如果两个链表相交,那么从相交位置开始,到结束,必有一段等长且相同的节点。假设链表 `listA` 的长度为 $m$、链表 `listB` 的长度为 $n,ドル他们的相交序列有 $k$ 个,则相交情况可以如下如所示:

![](http://qncdn.bujige.net/images/20210401113538.png)
![](https://qcdn.itcharge.cn/images/20210401113538.png)

现在问题是如何找到 m-k 或者 n-k 的位置。
现在问题是如何找到 $m - k$ 或者 $n - k$ 的位置。

考虑将链表 A 的末尾拼接上链表 B,链表 B 的末尾拼接上链表 A
考虑将链表 `listA` 的末尾拼接上链表 `listB`,链表 `listB` 的末尾拼接上链表 `listA`

然后使用两个指针 pA 、PB,分别从链表 A、链表 B 的头节点开始遍历,如果走到共同的节点,则返回该节点。
然后使用两个指针 `pA` 、`pB`,分别从链表 `listA`、链表 `listB` 的头节点开始遍历,如果走到共同的节点,则返回该节点。

否则走到两个链表末尾,返回 None。
否则走到两个链表末尾,返回 `None`

![image-20210401114058127](http://qncdn.bujige.net/images/20210401114100.png)
![](https://qcdn.itcharge.cn/images/20210401114100.png)

##代码
### 思路 1:代码

```Python
class Solution:
Expand All @@ -40,9 +74,14 @@ class Solution:
return None
pA = headA
pB = headB
while pA != pB:
while pA != pB:
pA = pA.next if pA != None else headB
pB = pB.next if pB != None else headA
return pA
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(m + n)$。
- **空间复杂度**:$O(1)$。

Loading

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