We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5016dfd commit b4f7592Copy full SHA for b4f7592
Linked-List/1721/solution2.js
@@ -3,25 +3,25 @@
3
* 空间复杂度:O(1)
4
*/
5
const swapNodes = function(head, k) {
6
- let currentHead = head;
+ let currentHead1 = head;
7
let currentHead2 = head;
8
- let x;
9
- while (currentHead) {
10
- if (k === 0) {
11
- currentHead2 = currentHead2.next;
12
- }
13
- if (k > 0) {
14
- if (k === 1) {
15
- x = currentHead;
16
17
- k--;
18
19
- currentHead = currentHead.next;
+
+ while (--k) {
+ currentHead1 = currentHead1.next;
20
}
21
+ const x = currentHead1;
+ while (currentHead1.next) {
+ currentHead2 = currentHead2.next;
+ }
+ const y = currentHead2
22
const temp = x.val;
23
- x.val = currentHead2.val;
24
- currentHead2.val = temp;
+ x.val = y.val;
+ y.val = temp;
25
26
return head;
27
};
0 commit comments