Skip to content

Commit eae8934

Browse files
committed
24-swap-nodes-in-pairs.md Separated Chinese and English solutions.
1 parent 1a65306 commit eae8934

File tree

3 files changed

+43
-232
lines changed

3 files changed

+43
-232
lines changed

en/1-1000/24-swap-nodes-in-pairs.md

+3-101
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
# 24. Swap Nodes in Pairs - Best Practices of LeetCode Solutions
2-
LeetCode link: [24. Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs),
3-
[24. 两两交换链表中的节点](https://leetcode.cn/problems/swap-nodes-in-pairs)
2+
LeetCode link: [24. Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs), difficulty: **Medium**
43

5-
[中文题解](#中文题解)
6-
7-
## LeetCode problem description
4+
## Description of "24. Swap Nodes in Pairs"
85
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
96

107
### [Example 1]
@@ -34,14 +31,12 @@ Given a linked list, swap every two adjacent nodes and return its head. You must
3431
- `0 <= Node.val <= 100`
3532

3633
## Intuition
37-
[中文题解](#中文题解)
38-
3934
Before solving this problem, it is recommended to solve the simple problem [206. Reverse Linked List](206-reverse-linked-list.md) first.
4035

4136
1. To solve this problem, you still need to define at least two variables: `current` and `previous`.
4237
2. The loop condition should be `while (current.next != null)` instead of `while (current != null)`, because the operations that need to be performed include `current.next.next`.
4338

44-
## Steps to the Solution
39+
## Steps
4540
1. Traverse all nodes.
4641
```c#
4742
var previous = null;
@@ -369,96 +364,3 @@ end
369364
```
370365
// Welcome to create a PR to complete the code of this language, thanks!
371366
```
372-
373-
## 问题描述
374-
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
375-
376-
### [Example 1]
377-
![](../../images/examples/24_1.jpg)
378-
379-
**输入**: `head = [1,2,3,4]`
380-
381-
**输出**: `[2,1,4,3]`
382-
383-
## 中文题解
384-
### 思路
385-
在做本题前,建议先完成简单题目[206. Reverse Linked List](206-reverse-linked-list.md)
386-
387-
1. 解决这个问题,依然至少需要定义两个变量:`current``previous`
388-
2. 循环条件应是`while (current.next != null)`,而不应该是`while (current != null)`,因为需要操作`current.next.next`.
389-
390-
### 步骤
391-
1. 遍历所有节点。
392-
```java
393-
var previous = null;
394-
var current = head;
395-
396-
while (current != null) {
397-
current = current.next;
398-
}
399-
```
400-
401-
2. 因为每两个节点进行一次位置互换,所以需要改为一次走两步。
402-
```java
403-
var previous = null;
404-
var current = head;
405-
406-
while (current != null && current.next != null) { // 1
407-
var nextNext = current.next.next; // 2
408-
current = nextNext; // 3
409-
}
410-
```
411-
412-
3. 交换 `current``current.next` 的位置。
413-
```java
414-
var previous = null;
415-
var current = head;
416-
417-
while (current != null && current.next != null) {
418-
var nextNext = current.next.next;
419-
420-
current.next.next = current; // 1
421-
current.next = nextNext; // 2
422-
423-
current = nextNext;
424-
}
425-
```
426-
427-
4. 处理 `previous`
428-
```java
429-
var previous = null;
430-
var current = head;
431-
432-
while (current != null && current.next != null) {
433-
var nextNext = current.next.next;
434-
435-
previous.next = current.next; // 1
436-
current.next.next = current;
437-
current.next = nextNext;
438-
439-
previous = current; // 2
440-
current = nextNext;
441-
}
442-
```
443-
444-
5. 确定返回值。因为`head`节点在节点数量超过1时,会被交换到第二个节点的位置,为了统一方便处理,最好加入`dummy_head`节点。
445-
```java
446-
var dummyHead = new ListNode(); // 1
447-
dummyHead.next = head; // 2
448-
449-
var previous = dummyHead; // 3
450-
var current = head;
451-
452-
while (current != null && current.next != null) {
453-
var nextNext = current.next.next;
454-
455-
previous.next = current.next;
456-
current.next.next = current;
457-
current.next = nextNext;
458-
459-
previous = current;
460-
current = nextNext;
461-
}
462-
463-
return dummyHead.next; // 4
464-
```

en/3001-4000/unorganized.md

+12
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,19 @@ The improved way with a queue is commonly more efficient. Relaxing **All Edges**
107107
- 530 https://leetcode.cn/problems/minimum-absolute-difference-in-bst/
108108
783 https://leetcode.com/problems/minimum-distance-between-bst-nodes/ is the same
109109
- 501 https://leetcode.cn/problems/find-mode-in-binary-search-tree/
110+
- 701 https://leetcode.cn/problems/insert-into-a-binary-search-tree/ Carl's solution is shorter, but may hard to understand and think about
111+
- 450 https://leetcode.cn/problems/delete-node-in-a-bst/ Carl's solution is shorter
112+
- 669 https://leetcode.cn/problems/trim-a-binary-search-tree/description/
113+
- 108 https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/
114+
- 538 https://leetcode.cn/problems/convert-bst-to-greater-tree/
115+
116+
### Backtracking
117+
- 77 https://leetcode.cn/problems/combinations/description/
118+
- 216 https://leetcode.cn/problems/combination-sum-iii/
119+
- 17 https://leetcode.cn/problems/letter-combinations-of-a-phone-number/
120+
- 39 https://leetcode.cn/problems/combination-sum/
110121
-
122+
111123
### Failed in 2 rounds
112124
- 222 https://leetcode.cn/problems/count-complete-tree-nodes/
113125
- 236 https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/

zh/1-1000/24-swap-nodes-in-pairs.md

+28-131
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,38 @@
1-
# 24. Swap Nodes in Pairs - Best Practices of LeetCode Solutions
2-
LeetCode link: [24. Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs),
3-
[24. 两两交换链表中的节点](https://leetcode.cn/problems/swap-nodes-in-pairs)
1+
# 24. 两两交换链表中的节点 - 力扣题解最佳实践
2+
力扣链接:[24. 两两交换链表中的节点](https://leetcode.cn/problems/swap-nodes-in-pairs), 难度: **中等**
43

5-
[中文题解](#中文题解)
6-
7-
## LeetCode problem description
8-
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
4+
## 力扣“24. 两两交换链表中的节点”问题描述
5+
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
96

10-
### [Example 1]
7+
### [示例 1]
118
![](../../images/examples/24_1.jpg)
129

13-
**Input**: `head = [1,2,3,4]`
14-
15-
**Output**: `[2,1,4,3]`
16-
17-
### [Example 2]
18-
**Input**: `head = []`
10+
**输入**: `head = [1,2,3,4]`
1911

20-
**Output**: `[]`
12+
**输出**: `[2,1,4,3]`
2113

22-
### [Example 3]
23-
**Input**: `head = [1]`
14+
### [示例 2]
15+
**输入**: `head = []`
2416

25-
**Output**: `[1]`
17+
**输出**: `[]`
2618

27-
### [Example 4]
28-
**Input**: `head = [1,2,3]`
19+
### [示例 3]
20+
**输入**: `head = [1]`
2921

30-
**Output**: `[2,1,3]`
22+
**输出**: `[1]`
3123

32-
### [Constraints]
33-
- The number of nodes in the list is in the range `[0, 100]`.
24+
### [约束]
25+
- 链表中节点的数目在范围 `[0, 100]`
3426
- `0 <= Node.val <= 100`
3527

36-
## Intuition
37-
[中文题解](#中文题解)
38-
39-
Before solving this problem, it is recommended to solve the simple problem [206. Reverse Linked List](206-reverse-linked-list.md) first.
28+
## 思路
29+
在做本题前,建议先完成简单题目[206. Reverse Linked List](206-reverse-linked-list.md)
4030

41-
1. To solve this problem, you still need to define at least two variables: `current` and `previous`.
42-
2. The loop condition should be `while (current.next != null)` instead of `while (current != null)`, because the operations that need to be performed include `current.next.next`.
31+
1. 解决这个问题,依然至少需要定义两个变量:`current``previous`
32+
2. 循环条件应是`while (current.next != null)`,而不应该是`while (current != null)`,因为需要操作`current.next.next`.
4333

44-
## Steps to the Solution
45-
1. Traverse all nodes.
34+
## 步骤
35+
1. 遍历所有节点。
4636
```c#
4737
var previous = null;
4838
var current = head;
@@ -52,7 +42,7 @@ while (current != null) {
5242
}
5343
```
5444

55-
2. Because every two nodes swap positions, it is necessary to change it to taking two steps at a time.
45+
2. 因为每两个节点进行一次位置互换,所以需要改为一次走两步。
5646
```c#
5747
var previous = null;
5848
var current = head;
@@ -63,7 +53,7 @@ while (current != null && current.next != null) { // 1
6353
}
6454
```
6555

66-
3. Swap the positions of `current` and `current.next`.
56+
3. 交换 `current` `current.next` 的位置。
6757
```c#
6858
var previous = null;
6959
var current = head;
@@ -78,7 +68,7 @@ while (current != null && current.next != null) {
7868
}
7969
```
8070

81-
4. Process `previous`.
71+
4. 处理 `previous`
8272
```c#
8373
var previous = null;
8474
var current = head;
@@ -95,7 +85,7 @@ while (current != null && current.next != null) {
9585
}
9686
```
9787

98-
5. Determine the return value. Because the `head` node will be swapped to the second node when the number of nodes exceeds 1, it is best to add a `dummy_head` node for unified and convenient processing.
88+
5. 确定返回值。因为`head`节点在节点数量超过1时,会被交换到第二个节点的位置,为了统一方便处理,最好加入`dummy_head`节点。
9989
```c#
10090
var dummyHead = new ListNode(); // 1
10191
dummyHead.next = head; // 2
@@ -117,9 +107,9 @@ while (current != null && current.next != null) {
117107
return dummyHead.next; // 4
118108
```
119109

120-
## Complexity
121-
* Time: `O(n)`.
122-
* Space: `O(1)`.
110+
## 复杂度
111+
* 时间: `O(n)`
112+
* 空间: `O(1)`
123113

124114
## Java
125115
```java
@@ -369,96 +359,3 @@ end
369359
```
370360
// Welcome to create a PR to complete the code of this language, thanks!
371361
```
372-
373-
## 问题描述
374-
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
375-
376-
### [Example 1]
377-
![](../../images/examples/24_1.jpg)
378-
379-
**输入**: `head = [1,2,3,4]`
380-
381-
**输出**: `[2,1,4,3]`
382-
383-
## 中文题解
384-
### 思路
385-
在做本题前,建议先完成简单题目[206. Reverse Linked List](206-reverse-linked-list.md)
386-
387-
1. 解决这个问题,依然至少需要定义两个变量:`current``previous`
388-
2. 循环条件应是`while (current.next != null)`,而不应该是`while (current != null)`,因为需要操作`current.next.next`.
389-
390-
### 步骤
391-
1. 遍历所有节点。
392-
```java
393-
var previous = null;
394-
var current = head;
395-
396-
while (current != null) {
397-
current = current.next;
398-
}
399-
```
400-
401-
2. 因为每两个节点进行一次位置互换,所以需要改为一次走两步。
402-
```java
403-
var previous = null;
404-
var current = head;
405-
406-
while (current != null && current.next != null) { // 1
407-
var nextNext = current.next.next; // 2
408-
current = nextNext; // 3
409-
}
410-
```
411-
412-
3. 交换 `current``current.next` 的位置。
413-
```java
414-
var previous = null;
415-
var current = head;
416-
417-
while (current != null && current.next != null) {
418-
var nextNext = current.next.next;
419-
420-
current.next.next = current; // 1
421-
current.next = nextNext; // 2
422-
423-
current = nextNext;
424-
}
425-
```
426-
427-
4. 处理 `previous`
428-
```java
429-
var previous = null;
430-
var current = head;
431-
432-
while (current != null && current.next != null) {
433-
var nextNext = current.next.next;
434-
435-
previous.next = current.next; // 1
436-
current.next.next = current;
437-
current.next = nextNext;
438-
439-
previous = current; // 2
440-
current = nextNext;
441-
}
442-
```
443-
444-
5. 确定返回值。因为`head`节点在节点数量超过1时,会被交换到第二个节点的位置,为了统一方便处理,最好加入`dummy_head`节点。
445-
```java
446-
var dummyHead = new ListNode(); // 1
447-
dummyHead.next = head; // 2
448-
449-
var previous = dummyHead; // 3
450-
var current = head;
451-
452-
while (current != null && current.next != null) {
453-
var nextNext = current.next.next;
454-
455-
previous.next = current.next;
456-
current.next.next = current;
457-
current.next = nextNext;
458-
459-
previous = current;
460-
current = nextNext;
461-
}
462-
463-
return dummyHead.next; // 4
464-
```

0 commit comments

Comments
 (0)