-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathRemoveNthNodeFromEndOfList.js
More file actions
110 lines (95 loc) 路 2.42 KB
/
Copy pathRemoveNthNodeFromEndOfList.js
File metadata and controls
110 lines (95 loc) 路 2.42 KB
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
107
108
109
110
/**
* Given a linked list, remove the nth node from the end of list and return its head.
*
* For example,
*
* Given linked list: 1->2->3->4->5, and n = 2.
*
* After removing the second node from the end, the linked list becomes 1->2->3->5.
* Note:
* Given n will always be valid.
* Try to do this in one pass.
*
* Accepted.
*/
function ListNode(val) {
this.val = val;
this.next = null;
this.equals = function (node) {
return this.next == null && node.next == null || this.val === node.val && (this.next != null) && this.next.equals(node.next);
}
}
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
let removeNthFromEnd = function(head, n) {
let stack = [];
let tmp = null, node = head;
for (; node != null; node = node.next) {
stack.push(node);
}
for (; n > 0 && stack.length !== 0; n--) {
tmp = stack[stack.length -1];
stack.splice(stack.length - 1);
}
if (tmp != null) {
if (stack.length !== 0) {
stack[stack.length - 1].next = tmp.next;
} else {
head = tmp.next;
}
}
return head;
};
let node12345 = new ListNode(1);
node12345.next = new ListNode(2);
node12345.next.next = new ListNode(3);
node12345.next.next.next = new ListNode(4);
node12345.next.next.next.next = new ListNode(5);
let tmp = new ListNode(1);
tmp.next = new ListNode(2);
tmp.next.next = new ListNode(3);
tmp.next.next.next = new ListNode(5);
if(removeNthFromEnd(node12345, 2).equals(tmp)){
console.log("pass")
} else {
console.error("failed")
}
let node12 = new ListNode(1);
node12.next = new ListNode(2);
tmp = new ListNode(1);
if(removeNthFromEnd(node12, 1).equals(tmp)) {
console.log("pass")
} else {
console.error("failed")
}
if (removeNthFromEnd(new ListNode(1), 1) === null) {
console.log("pass")
} else {
console.error("failed")
}
if (removeNthFromEnd(null, 2) === null) {
console.log("pass")
} else {
console.error("failed")
}
let node123 = new ListNode(1);
node123.next = new ListNode(2);
node123.next.next = new ListNode(3);
tmp = new ListNode(2);
tmp.next = new ListNode(3);
node123 = removeNthFromEnd(node123, 3);
if(node123, tmp);
tmp = new ListNode(2);
if(removeNthFromEnd(node123, 1).equals(tmp)) {
console.log("pass")
} else {
console.error("failed")
}
if (removeNthFromEnd(node123, 1) === null) {
console.log("pass")
} else {
console.error("failed")
}