-
Notifications
You must be signed in to change notification settings - Fork 0
/
0024. Swap Nodes in Pairs.js
109 lines (98 loc) · 2.61 KB
/
0024. 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Given a linked list, swap every two adjacent nodes and return its head.
// You may not modify the values in the list's nodes, only nodes itself may be changed.
// Example:
// Given 1->2->3->4, you should return the list as 2->1->4->3.
// 1) 递归
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
function ListNode(val) {
this.val = val
this.next = null
}
const swapPairs = (head) => {
if (!head || !head.next) {
return head
}
let res = head.next
head.next = swapPairs(head.next.next)
res.next = head
return res
}
// Runtime: 56 ms, faster than 59.42% of JavaScript online submissions for Swap Nodes in Pairs.
// Memory Usage: 33.8 MB, less than 78.95% of JavaScript online submissions for Swap Nodes in Pairs.
// 2) 迭代
// dummy:
// ① cur first sec
// null -> 1 -> 2 -> 3 -> 4 -> null
// ② cur.next = sec
// --------------->
// null 1 -> 2 -> 3 -> 4 -> null
// ③ first.next = sec.next
// --------------->
// null 1 2 -> 3 -> 4 -> null
// ------------>
// ④ sec.next = first
// --------------->
// null 1 <- 2 -> 3 -> 4 -> null
// ------------>
// ⑤ cur = cur.next.next
// cur
// null -> 2 -> 1 -> 3 -> 4 -> null
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
function ListNode(val) {
this.val = val
this.next = null
}
const swapPairs = (head) => {
if (!head || !head.next) {
return head
}
let dummy = new ListNode(null)
dummy.next = head
cur = dummy // null -> 1 -> 2 -> 3 -> 4
while (cur.next && cur.next.next) {
let first = cur.next // first: 1 -> 2 -> 3 -> 4
let sec = cur.next.next // sec: 2 -> 3 -> 4
cur.next = sec // cur: null -> 2 -> 3 -> 4
first.next = sec.next // first: 1 -> 3 -> 4
sec.next = first // sec: 2 -> 1 -> 3 -> 4
cur = cur.next.next // dummy: null -> 2 -> 1 -> 3 -> 4, cur: 2 -> 3 -> 4
}
return dummy.next
}
// Runtime: 56 ms, faster than 59.42% of JavaScript online submissions for Swap Nodes in Pairs.
// Memory Usage: 33.7 MB, less than 94.74% of JavaScript online submissions for Swap Nodes in Pairs.
// Test case:
// let head1 = {
// val: 1,
// next: {
// val: 2,
// next: {
// val: 3,
// next: {
// val: 4,
// next: null
// }
// }
// }
// }
// console.log(swapPairs(head1))