-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
58 lines (56 loc) · 1.24 KB
/
index.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
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
function ListNode(val) {
this.val = val;
this.next = null;
}
const head_node = new ListNode(1);
head_node.next = new ListNode(2);
head_node.next.next = new ListNode(3);
head_node.next.next.next = new ListNode(4);
head_node.next.next.next.next = new ListNode(5);
/**
* 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) {
let current_node = head;
const node = [];
while (current_node.next) {
node.push(current_node);
current_node = current_node.next;
}
node.push(current_node);
if (n > node.length) {
return null;
}
if (n === node.length) {
return node[0].next;
}
node[node.length - n - 1].next = node[node.length - n +1];
return head;
};
let temp_node = removeNthFromEnd(head_node, 2);
while(temp_node.next) {
console.log(temp_node.val);
temp_node = temp_node.next;
}
console.log(temp_node.val);