-
Notifications
You must be signed in to change notification settings - Fork 0
/
swapPairs.js
52 lines (47 loc) · 999 Bytes
/
swapPairs.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
'use strict';
/**
* Definition for singly-linked list.
*/
function ListNode(val) {
this.val = val;
this.next = null;
}
/**
* @param {ListNode} head
* @returns {ListNode}
*/
var swapPairs = function (head) {
var ahead = new ListNode(null);
ahead.next = head;
var a = ahead;
var b = head;
var c = b && b.next
if (!b || !c) return head;
// update head
head = c;
while (true) {
b.next = c.next;
a.next = c;
c.next = b;
// update a, b, and c
a = b;
b = b.next;
c = b && b.next;
if (!b || !c) break;
}
return head;
};
var assert = require('assert');
var a = new ListNode('a');
var b = new ListNode('b');
var c = new ListNode('c');
var d = new ListNode('d');
a.next = b;
b.next = c;
c.next = d;
var list = swapPairs(a);
console.log(list.next.val);
assert(list.val === 'b');
assert(list.next.val === 'a');
assert(list.next.next.val === 'd');
assert(list.next.next.next.val === 'c');