File tree 6 files changed +115
-43
lines changed
solution/0000-0099/0024.Swap Nodes in Pairs
6 files changed +115
-43
lines changed Original file line number Diff line number Diff line change 7
7
<a href =" https://opencollective.com/doocs-leetcode/backers/badge.svg " alt =" backers on Open Collective " ><img src =" https://opencollective.com/doocs-leetcode/backers/badge.svg " /></a >
8
8
<a href =" https://opencollective.com/doocs-leetcode/sponsors/badge.svg " alt =" Sponsors on Open Collective " ><img src =" https://opencollective.com/doocs-leetcode/sponsors/badge.svg " /></a >
9
9
<a href =" https://github.com/doocs/leetcode/blob/main/LICENSE " ><img src =" https://badgen.net/github/license/doocs/leetcode?color=green " alt =" LICENSE " ></a ><br >
10
- <a href =" https://github.com/doocs/leetcode/stargazers " ><img src =" https://badgen.net/github/stars/doocs/leetcode?color=cyan " alt =" stars " ></a >
11
- <a href =" https://github.com/doocs/leetcode/network/members " ><img src =" https://badgen.net/github/forks/doocs/leetcode?color=cyan " alt =" forks " ></a >
10
+ <a href =" https://github.com/doocs/leetcode/stargazers " ><img src =" https://badgen.net/github/stars/doocs/leetcode?color=cyan&cache=300 " alt =" stars " ></a >
11
+ <a href =" https://github.com/doocs/leetcode/network/members " ><img src =" https://badgen.net/github/forks/doocs/leetcode?color=cyan&cache=300 " alt =" forks " ></a >
12
12
<a href =" https://github.com/doocs/leetcode " ><img src =" https://badgen.net/badge/⭐/GitHub/cyan " alt =" github " ></a >
13
13
<a href =" https://gitee.com/doocs/leetcode " ><img src =" https://badgen.net/badge/⭐/Gitee/cyan " alt =" github " ></a >
14
14
<a href =" http://makeapullrequest.com " ><img src =" https://badgen.net/badge/PRs/welcome/cyan " alt =" PRs Welcome " ></a >
Original file line number Diff line number Diff line change @@ -37,12 +37,12 @@ class Solution:
37
37
dummy = ListNode(next = head)
38
38
pre, cur = dummy, head
39
39
while cur and cur.next:
40
- pre.next = cur.next
41
- t = cur.next.next
42
- cur.next .next = cur
43
- cur .next = t
40
+ t = cur.next
41
+ cur.next = t .next
42
+ t .next = cur
43
+ pre .next = t
44
44
pre = cur
45
- cur = cur .next
45
+ cur = pre .next
46
46
return dummy.next
47
47
```
48
48
@@ -66,18 +66,51 @@ class Solution {
66
66
ListNode dummy = new ListNode (0 , head);
67
67
ListNode pre = dummy, cur = head;
68
68
while (cur != null && cur. next != null ) {
69
- pre . next = cur. next;
70
- ListNode t = cur. next. next;
71
- cur . next . next = cur;
72
- cur . next = t;
69
+ ListNode t = cur. next;
70
+ cur. next = t . next;
71
+ t . next = cur;
72
+ pre . next = t;
73
73
pre = cur;
74
- cur = cur. next;
74
+ cur = pre. next;
75
+
75
76
}
76
77
return dummy. next;
77
78
}
78
79
}
79
80
```
80
81
82
+ ### ** C++**
83
+
84
+ ``` cpp
85
+ /* *
86
+ * Definition for singly-linked list.
87
+ * struct ListNode {
88
+ * int val;
89
+ * ListNode *next;
90
+ * ListNode() : val(0), next(nullptr) {}
91
+ * ListNode(int x) : val(x), next(nullptr) {}
92
+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
93
+ * };
94
+ */
95
+ class Solution {
96
+ public:
97
+ ListNode* swapPairs(ListNode* head) {
98
+ ListNode* dummy = new ListNode(0, head);
99
+ ListNode* pre = dummy;
100
+ ListNode* cur = head;
101
+ while (cur != nullptr && cur->next != nullptr) {
102
+ ListNode* t = cur->next;
103
+ cur->next = t->next;
104
+ t->next = cur;
105
+ pre->next = t;
106
+ pre = cur;
107
+ cur = pre->next;
108
+ }
109
+ return dummy->next;
110
+ }
111
+ };
112
+ ```
113
+
81
114
### **...**
82
115
83
116
```
Original file line number Diff line number Diff line change @@ -35,12 +35,12 @@ class Solution:
35
35
dummy = ListNode(next = head)
36
36
pre, cur = dummy, head
37
37
while cur and cur.next:
38
- pre.next = cur.next
39
- t = cur.next.next
40
- cur.next .next = cur
41
- cur .next = t
38
+ t = cur.next
39
+ cur.next = t .next
40
+ t .next = cur
41
+ pre .next = t
42
42
pre = cur
43
- cur = cur .next
43
+ cur = pre .next
44
44
return dummy.next
45
45
```
46
46
@@ -62,18 +62,51 @@ class Solution {
62
62
ListNode dummy = new ListNode (0 , head);
63
63
ListNode pre = dummy, cur = head;
64
64
while (cur != null && cur. next != null ) {
65
- pre . next = cur. next;
66
- ListNode t = cur. next. next;
67
- cur . next . next = cur;
68
- cur . next = t;
65
+ ListNode t = cur. next;
66
+ cur. next = t . next;
67
+ t . next = cur;
68
+ pre . next = t;
69
69
pre = cur;
70
- cur = cur. next;
70
+ cur = pre. next;
71
+
71
72
}
72
73
return dummy. next;
73
74
}
74
75
}
75
76
```
76
77
78
+ ### ** C++**
79
+
80
+ ``` cpp
81
+ /* *
82
+ * Definition for singly-linked list.
83
+ * struct ListNode {
84
+ * int val;
85
+ * ListNode *next;
86
+ * ListNode() : val(0), next(nullptr) {}
87
+ * ListNode(int x) : val(x), next(nullptr) {}
88
+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
89
+ * };
90
+ */
91
+ class Solution {
92
+ public:
93
+ ListNode* swapPairs(ListNode* head) {
94
+ ListNode* dummy = new ListNode(0, head);
95
+ ListNode* pre = dummy;
96
+ ListNode* cur = head;
97
+ while (cur != nullptr && cur->next != nullptr) {
98
+ ListNode* t = cur->next;
99
+ cur->next = t->next;
100
+ t->next = cur;
101
+ pre->next = t;
102
+ pre = cur;
103
+ cur = pre->next;
104
+ }
105
+ return dummy->next;
106
+ }
107
+ };
108
+ ```
109
+
77
110
### **...**
78
111
79
112
```
Original file line number Diff line number Diff line change 3
3
* struct ListNode {
4
4
* int val;
5
5
* ListNode *next;
6
- * ListNode(int x) : val(x), next(NULL) {}
6
+ * ListNode() : val(0), next(nullptr) {}
7
+ * ListNode(int x) : val(x), next(nullptr) {}
8
+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
7
9
* };
8
10
*/
9
11
class Solution {
10
12
public:
11
13
ListNode* swapPairs (ListNode* head) {
12
- if (head==NULL ||head->next ==NULL )// 递归的最小情况,链表为空,或者长度为1,直接返回
13
- return head;
14
- ListNode *temp=head->next ;// 定义temp为head->next的node
15
- ListNode *nextChain=swapPairs (head->next ->next );// 递归调用得到head->next->next链表交换结果
16
- head->next =nextChain;// head的next指向递归后的结果
17
- temp->next =head;// temp也就是之前的head->next节点指向head,那么现在的head为temp
18
- return temp;
14
+ ListNode* dummy = new ListNode (0 , head);
15
+ ListNode* pre = dummy;
16
+ ListNode* cur = head;
17
+ while (cur != nullptr && cur->next != nullptr ) {
18
+ ListNode* t = cur->next ;
19
+ cur->next = t->next ;
20
+ t->next = cur;
21
+ pre->next = t;
22
+ pre = cur;
23
+ cur = pre->next ;
24
+ }
25
+ return dummy->next ;
19
26
}
20
- };
21
-
22
-
27
+ };
Original file line number Diff line number Diff line change @@ -13,12 +13,13 @@ public ListNode swapPairs(ListNode head) {
13
13
ListNode dummy = new ListNode (0 , head );
14
14
ListNode pre = dummy , cur = head ;
15
15
while (cur != null && cur .next != null ) {
16
- pre . next = cur .next ;
17
- ListNode t = cur .next .next ;
18
- cur . next .next = cur ;
19
- cur .next = t ;
16
+ ListNode t = cur .next ;
17
+ cur .next = t .next ;
18
+ t .next = cur ;
19
+ pre .next = t ;
20
20
pre = cur ;
21
- cur = cur .next ;
21
+ cur = pre .next ;
22
+
22
23
}
23
24
return dummy .next ;
24
25
}
Original file line number Diff line number Diff line change @@ -8,10 +8,10 @@ def swapPairs(self, head: ListNode) -> ListNode:
8
8
dummy = ListNode (next = head )
9
9
pre , cur = dummy , head
10
10
while cur and cur .next :
11
- pre . next = cur .next
12
- t = cur .next .next
13
- cur . next .next = cur
14
- cur .next = t
11
+ t = cur .next
12
+ cur .next = t .next
13
+ t .next = cur
14
+ pre .next = t
15
15
pre = cur
16
- cur = cur .next
16
+ cur = pre .next
17
17
return dummy .next
You can’t perform that action at this time.
0 commit comments