4646
4747<p ><strong >进阶:</strong >你能在不修改链表节点值的情况下解决这个问题吗?(也就是说,仅修改节点本身。)</p >
4848
49-
5049## 解法
5150
5251<!-- 这里可写通用的实现逻辑 -->
5352
53+ 设置虚拟头节点 dummy,pre 指针初始指向 dummy,遍历链表,每次交换 pre 后面的两个节点即可。
54+
5455<!-- tabs:start -->
5556
5657### ** Python3**
@@ -72,8 +73,7 @@ class Solution:
7273 cur.next = t.next
7374 t.next = cur
7475 pre.next = t
75- pre = cur
76- cur = pre.next
76+ pre, cur = cur, cur.next
7777 return dummy.next
7878```
7979
@@ -102,14 +102,43 @@ class Solution {
102102 t. next = cur;
103103 pre. next = t;
104104 pre = cur;
105- cur = pre. next;
106-
105+ cur = cur. next;
107106 }
108107 return dummy. next;
109108 }
110109}
111110```
112111
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+
113142### ** C++**
114143
115144``` cpp
@@ -126,22 +155,75 @@ class Solution {
126155class Solution {
127156public:
128157 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;
132160 while (cur != nullptr && cur->next != nullptr) {
133- ListNode* t = cur->next;
161+ ListNode * t = cur->next;
134162 cur->next = t->next;
135163 t->next = cur;
136164 pre->next = t;
137165 pre = cur;
138- cur = pre ->next;
166+ cur = cur ->next;
139167 }
140168 return dummy->next;
141169 }
142170};
143171```
144172
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+
145227### ** ...**
146228
147229```
0 commit comments