46
46
47
47
<p ><strong >进阶:</strong >你能在不修改链表节点值的情况下解决这个问题吗?(也就是说,仅修改节点本身。)</p >
48
48
49
-
50
49
## 解法
51
50
52
51
<!-- 这里可写通用的实现逻辑 -->
53
52
53
+ 设置虚拟头节点 dummy,pre 指针初始指向 dummy,遍历链表,每次交换 pre 后面的两个节点即可。
54
+
54
55
<!-- tabs:start -->
55
56
56
57
### ** Python3**
@@ -72,8 +73,7 @@ class Solution:
72
73
cur.next = t.next
73
74
t.next = cur
74
75
pre.next = t
75
- pre = cur
76
- cur = pre.next
76
+ pre, cur = cur, cur.next
77
77
return dummy.next
78
78
```
79
79
@@ -102,14 +102,43 @@ class Solution {
102
102
t. next = cur;
103
103
pre. next = t;
104
104
pre = cur;
105
- cur = pre. next;
106
-
105
+ cur = cur. next;
107
106
}
108
107
return dummy. next;
109
108
}
110
109
}
111
110
```
112
111
112
+ ### ** JavaScript**
113
+
114
+ ``` js
115
+ /**
116
+ * Definition for singly-linked list.
117
+ * function ListNode(val, next) {
118
+ * this.val = (val===undefined ? 0 : val)
119
+ * this.next = (next===undefined ? null : next)
120
+ * }
121
+ */
122
+ /**
123
+ * @param {ListNode} head
124
+ * @return {ListNode}
125
+ */
126
+ var swapPairs = function (head ) {
127
+ const dummy = new ListNode (0 , head);
128
+ let pre = dummy;
129
+ let cur = head;
130
+ while (cur && cur .next ) {
131
+ const t = cur .next ;
132
+ cur .next = t .next ;
133
+ t .next = cur;
134
+ pre .next = t;
135
+ pre = cur;
136
+ cur = cur .next ;
137
+ }
138
+ return dummy .next ;
139
+ };
140
+ ```
141
+
113
142
### ** C++**
114
143
115
144
``` cpp
@@ -126,22 +155,75 @@ class Solution {
126
155
class Solution {
127
156
public:
128
157
ListNode* swapPairs(ListNode* head) {
129
- ListNode* dummy = new ListNode(0, head);
130
- ListNode* pre = dummy;
131
- ListNode* cur = head;
158
+ ListNode * dummy = new ListNode(0, head);
159
+ ListNode * pre = dummy, * cur = head;
132
160
while (cur != nullptr && cur->next != nullptr) {
133
- ListNode* t = cur->next;
161
+ ListNode * t = cur->next;
134
162
cur->next = t->next;
135
163
t->next = cur;
136
164
pre->next = t;
137
165
pre = cur;
138
- cur = pre ->next;
166
+ cur = cur ->next;
139
167
}
140
168
return dummy->next;
141
169
}
142
170
};
143
171
```
144
172
173
+ ### **Go**
174
+
175
+ ```go
176
+ /**
177
+ * Definition for singly-linked list.
178
+ * type ListNode struct {
179
+ * Val int
180
+ * Next *ListNode
181
+ * }
182
+ */
183
+ func swapPairs(head *ListNode) *ListNode {
184
+ dummy := &ListNode{0, head}
185
+ pre, cur := dummy, head
186
+ for cur != nil && cur.Next != nil {
187
+ t := cur.Next
188
+ cur.Next = t.Next
189
+ t.Next = cur
190
+ pre.Next = t
191
+ pre = cur
192
+ cur = cur.Next
193
+ }
194
+ return dummy.Next
195
+ }
196
+ ```
197
+
198
+ ### ** Ruby**
199
+
200
+ ``` rb
201
+ # Definition for singly-linked list.
202
+ # class ListNode
203
+ # attr_accessor :val, :next
204
+ # def initialize(val = 0, _next = nil)
205
+ # @val = val
206
+ # @next = _next
207
+ # end
208
+ # end
209
+ # @ param {ListNode} head
210
+ # @ return {ListNode}
211
+ def swap_pairs (head )
212
+ dummy = ListNode .new (0 , head)
213
+ pre = dummy
214
+ cur = head
215
+ while ! cur.nil? && ! cur.next.nil?
216
+ t = cur.next
217
+ cur.next = t.next
218
+ t.next = cur
219
+ pre.next = t
220
+ pre = cur
221
+ cur = cur.next
222
+ end
223
+ dummy.next
224
+ end
225
+ ```
226
+
145
227
### ** ...**
146
228
147
229
```
0 commit comments