forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
49 lines (48 loc) · 877 Bytes
/
Copy pathmain.js
File metadata and controls
49 lines (48 loc) · 877 Bytes
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
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function (head, n) {
const dummy = new ListNode(-1)
dummy.next = head
const points = [dummy]
let cur = head
while (cur) {
points.push(cur)
cur = cur.next
}
const prev = points[points.length - n - 1]
prev.next = prev.next.next
return dummy.next
}
if (process.env.LZS) { // local test
function ListNode (val) {
this.val = val
this.next = null
}
const assert = require('chai').assert
assert.deepStrictEqual(removeNthFromEnd({
val: 1,
next: {
val: 2,
next: {
val: 3,
next: null
}
}
}, 3), {
val: 2,
next: {
val: 3,
next: null
}
})
}