You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
LeetCode link: [24. Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs), difficulty: **Medium**
4
3
5
-
[中文题解](#中文题解)
6
-
7
-
## LeetCode problem description
4
+
## Description of "24. Swap Nodes in Pairs"
8
5
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.)
9
6
10
7
### [Example 1]
@@ -34,14 +31,12 @@ Given a linked list, swap every two adjacent nodes and return its head. You must
34
31
-`0 <= Node.val <= 100`
35
32
36
33
## Intuition
37
-
[中文题解](#中文题解)
38
-
39
34
Before solving this problem, it is recommended to solve the simple problem [206. Reverse Linked List](206-reverse-linked-list.md) first.
40
35
41
36
1. To solve this problem, you still need to define at least two variables: `current` and `previous`.
42
37
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`.
43
38
44
-
## Steps to the Solution
39
+
## Steps
45
40
1. Traverse all nodes.
46
41
```c#
47
42
varprevious=null;
@@ -369,96 +364,3 @@ end
369
364
```
370
365
// Welcome to create a PR to complete the code of this language, thanks!
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.)
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`.
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.
0 commit comments