Skip to content

Commit 79e104a

Browse files
committed
solve: remove nth node from end of list
1 parent 9ae0b36 commit 79e104a

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

โ€Žremove-nth-node-from-end-of-list/wogha95.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,49 @@
11
/**
2+
* 2์ฐจ
3+
* n๋งŒํผ first๊ฐ€ ์›€์ง์ธ ๋‹ค์Œ, first๊ฐ€ ๋๊นŒ์ง€ ๊ฐˆ๋•Œ๊นŒ์ง€ second๋ฅผ ์›€์ง์ž…๋‹ˆ๋‹ค.
4+
* ๊ทธ๋Ÿผ ๊ทธ ์œ„์น˜๊ฐ€ ๋์—์„œ n๋ฒˆ์งธ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋Š” ํ’€์ด์ž…๋‹ˆ๋‹ค.
5+
*
6+
* TC: O(N)
7+
* SC: O(1)
8+
* N: list length
9+
*/
10+
11+
/**
12+
* Definition for singly-linked list.
13+
* function ListNode(val, next) {
14+
* this.val = (val===undefined ? 0 : val)
15+
* this.next = (next===undefined ? null : next)
16+
* }
17+
*/
18+
/**
19+
* @param {ListNode} head
20+
* @param {number} n
21+
* @return {ListNode}
22+
*/
23+
var removeNthFromEnd = function (head, n) {
24+
const resultHead = new ListNode(null, head);
25+
let first = resultHead;
26+
let second = resultHead;
27+
28+
while (n > 0) {
29+
first = first.next;
30+
n -= 1;
31+
}
32+
33+
while (first.next) {
34+
first = first.next;
35+
second = second.next;
36+
}
37+
38+
second.next = second.next.next;
39+
40+
return resultHead.next;
41+
};
42+
43+
/**
44+
* 1์ฐจ
45+
* ์ „์ฒด ์ˆœํšŒ๋กœ ๊ฐฏ์ˆ˜๋ฅผ ํŒŒ์•…ํ•˜๊ณ  ํ•ด๋‹น ์œ„์น˜๋กœ๊ฐ€์„œ ๋งํฌ ์—ฐ๊ฒฐ ์ˆ˜์ • ์ž‘์—…
46+
*
247
* TC: O(N)
348
* SC: O(1)
449
* N: list length

0 commit comments

Comments
ย (0)