Skip to content

Commit caa021e

Browse files
BarklimBarklim
Barklim
authored and
Barklim
committed
Create 0019.js
1 parent 60bf56c commit caa021e

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

0019-remove-nth-node-from-end-of-list.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,45 @@ var removeNthFromEnd = function(head, n) {
3838

3939
return result.next;
4040
};
41+
42+
// var removeNthFromEnd = function(head, n) {
43+
// const result = new LinkedListNode();
44+
// let slow = result;
45+
// let fast = result;
46+
// slow.next = head;
47+
48+
// for (let i = 0; i <= n; i++) {
49+
// fast = fast.next;
50+
// }
51+
52+
// while (fast) {
53+
// fast = fast.next;
54+
// slow = slow.next;
55+
// }
56+
57+
// slow.next = slow.next.next;
58+
59+
// return result.next;
60+
// };
61+
62+
63+
// var removeNthFromEnd = function(head, n) {
64+
// const dummy = new LinkedListNode();
65+
// dummy.next = head
66+
67+
// first = dummy
68+
// second = dummy
69+
70+
// for (let i = 0; i <= n; i++) {
71+
// first = first.next;
72+
// }
73+
74+
// while (first) {
75+
// first = first.next;
76+
// second = second.next;
77+
// }
78+
79+
// second.next = second.next.next;
80+
81+
// return dummy.next;
82+
// };

example/3.Linkedlist/0019.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @param {number} n
11+
* @return {ListNode}
12+
*/
13+
var removeNthFromEnd = function(head, n) {
14+
15+
};
16+
17+
const myLinkedList = new LinkedList();
18+
myLinkedList.prepend(1);
19+
myLinkedList.append(2);
20+
myLinkedList.append(3);
21+
myLinkedList.append(4);
22+
myLinkedList.append(5);
23+
24+
const myLinkedList2 = new LinkedList();
25+
myLinkedList2.prepend(1);
26+
27+
const myLinkedList3 = new LinkedList();
28+
myLinkedList3.prepend(1);
29+
myLinkedList3.append(2);
30+
31+
const myLinkedList4 = new LinkedList();
32+
myLinkedList4.prepend(1);
33+
myLinkedList4.append(2);
34+
myLinkedList4.append(3);
35+
myLinkedList4.append(4);
36+
myLinkedList4.append(5);
37+
myLinkedList4.append(6);
38+
myLinkedList4.append(7);
39+
40+
const executeList = (list, n) => removeNthFromEnd(list.head, n)
41+
42+
const execute = (list, n) => {
43+
console.log('travers')
44+
console.log(list.toString());
45+
const list1 = executeList(list, n)
46+
console.log(traversList(list1))
47+
}
48+
49+
execute(myLinkedList, 2) // 1,2,3,4,5 // 1,2,3,5
50+
execute(myLinkedList2, 1) // 1 // []
51+
execute(myLinkedList3, 1) // 1,2 // 1
52+
execute(myLinkedList4, 4) // 1,2,3,4,5,6,7 // 1,2,3,5,6,7
53+
execute(myLinkedList4, 2) // 1,2,3,5,6,7 // 1,2,3,5,7
54+
55+

0 commit comments

Comments
 (0)