-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path24.zy445566.js
43 lines (43 loc) · 1.09 KB
/
24.zy445566.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var swapPairs = function(head) {
function swapHeadNode(nowNode) {
if (nowNode.next!=null) {
let tmpNode = nowNode.next;
nowNode.next = nowNode.next.next;
tmpNode.next = nowNode;
return [tmpNode,nowNode];
}
return null;
}
function swapNode(nowNode) {
let nowPNode = nowNode;
nowNode = nowNode.next;
if (nowNode!=null && nowNode.next!=null) {
let tmpNode = nowNode.next;
nowNode.next = nowNode.next.next;
tmpNode.next = nowNode;
nowPNode.next = tmpNode;
return nowNode;
}
return null;
}
if (head==null) {return null;}
if (head.next ==null) {return head;}
let resHead = swapHeadNode(head);
head = resHead[0];
let nowNode = resHead[1];
while(nowNode!=null) {
nowNode = swapNode(nowNode);
}
return head;
};