-
Notifications
You must be signed in to change notification settings - Fork 4
/
24.Swap_Nodes_in_Pairs.js
63 lines (62 loc) · 1.56 KB
/
24.Swap_Nodes_in_Pairs.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var swapPairs = function (head) {
/**
* 思路:
* 逢偶数新建链表,奇数索引收集短链表
* 遍历短链表并交换
* 重新链接链表
* 注意奇数个用例:[1],[1,2,3]
*/
let i = 0;
const shortListnodes = [];
let shortListnode = null;
while (head) {
if (i % 2 === 0) {
shortListnode = new ListNode(head.val);
}
if (i % 2 === 1) {
shortListnode.next = new ListNode(head.val);
shortListnodes.push(shortListnode);
shortListnode = null;
}
head = head.next;
i++;
}
let finalListnode = new ListNode();
const initialListnode = finalListnode;
if (shortListnodes.length > 0) {
const switchNodes = shortListnodes.map(switchListnode);
for (const node of switchNodes) {
finalListnode.next = node;
finalListnode = node.next;
}
}
if (shortListnode) finalListnode.next = shortListnode;
return initialListnode.next;
/**
* 交换两个节点
*/
// 方法1:新节点
// function switchListnode(listnode) {
// const headListnode = new ListNode(listnode.val);
// const nextListnode = new ListNode(listnode.next.val);
// nextListnode.next = headListnode;
// return nextListnode;
// }
// 方法2:原始节点
function switchListnode(listnode) {
listnode.next.next = new ListNode(listnode.val);
listnode = listnode.next;
return listnode;
}
};