Skip to content

Commit 203946b

Browse files
committed
add solution : 19. Remove Nth Node From End of List
1 parent 1ec2d40 commit 203946b

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
class ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
constructor(val?: number, next?: ListNode | null) {
5+
this.val = val === undefined ? 0 : val;
6+
this.next = next === undefined ? null : next;
7+
}
8+
}
9+
10+
/**
11+
*@link https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
12+
*
13+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
14+
* - ๋ฆฌ์ŠคํŠธ ์ˆœํšŒํ•ด์„œ ๋ฆฌ์ŠคํŠธ ์‚ฌ์ด์ฆˆ ์•Œ์•„๋‚ธ ๋’ค, ์•ž์—์„œ๋ถ€ํ„ฐ ๋ช‡ ๋ฒˆ์งธ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•ด์•ผํ•˜๋Š”์ง€ ์•Œ์•„๋‚ด๊ธฐ
15+
* - ๋ฆฌ์ŠคํŠธ ๋‹ค์‹œ ์ˆœํšŒํ•ด์„œ ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ๋…ธ๋“œ ์ œ๊ฑฐํ•˜๊ธฐ
16+
*
17+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
18+
* - n = ๋ฆฌ์ŠคํŠธ ๊ธธ์ด
19+
* - 1ํšŒ ์ˆœํšŒ -> ๋ฆฌ์ŠคํŠธ ๊ธธ์ด ์•Œ์•„๋‚ด๊ธฐ, ์ถ”๊ฐ€๋กœ 1ํšŒ ์ˆœํšŒํ•ด์„œ ๋…ธ๋“œ ์ œ๊ฑฐ
20+
*
21+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(1)
22+
* - ๊ณ ์ •๋œ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉ
23+
*/
24+
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
25+
let listSize = 0;
26+
let current = head;
27+
28+
// ๋ฆฌ์ŠคํŠธ ๊ธธ์ด ๊ตฌํ•˜๊ธฐ
29+
while (current) {
30+
listSize++;
31+
current = current.next;
32+
}
33+
34+
// ์ œ๊ฑฐํ•  ๋…ธ๋“œ์˜ ์ธ๋ฑ์Šค
35+
const targetIndex = listSize - n;
36+
let currentIndex = 0;
37+
38+
current = head;
39+
40+
if (targetIndex === 0) return head ? head.next : null;
41+
42+
while (current) {
43+
if (currentIndex === targetIndex - 1 && current.next) {
44+
current.next = current.next.next;
45+
break;
46+
}
47+
currentIndex++;
48+
current = current.next;
49+
}
50+
51+
return head;
52+
}
53+
54+
/**
55+
*
56+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• : two pointer ์‚ฌ์šฉ
57+
* - ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ๊ฐ€ ์ œ๊ฑฐ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•˜๋‹ˆ๊นŒ, dummy ๋…ธ๋“œ ๋งŒ๋“ค์–ด์„œ ํ—ค๋“œ ๋…ธ๋“œ๋กœ ์„ค์ •
58+
* - fast ํฌ์ธํ„ฐ์˜ ์ดˆ๊ธฐ ์œ„์น˜๋ฅผ n+1๋กœ ์„ค์ •ํ•˜๊ธฐ
59+
* - ์ดํ›„์—๋Š” slow ํฌ์ธํ„ฐ์™€ fast ํฌ์ธํ„ฐ ๋ชจ๋‘ 1๊ฐœ์”ฉ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ˆœํšŒ
60+
* - fast ํฌ์ธํ„ฐ๊ฐ€ ๋์— ๋„๋‹ฌํ•˜๋ฉด, slow ํฌ์ธํ„ฐ๋Š” ์‚ญ์ œํ•  ๋…ธ๋“œ ์ด์ „ ๋…ธ๋“œ์— ๋„๋‹ฌ
61+
*
62+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
63+
* - n = ๋ฆฌ์ŠคํŠธ ๊ธธ์ด
64+
* - 1ํšŒ ์ˆœํšŒ๋กœ ์‚ญ์ œํ•  ๋…ธ๋“œ ์•Œ์•„๋‚ด๊ธฐ
65+
*
66+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(1)
67+
* - ๊ณ ์ •๋œ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉ
68+
*/
69+
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
70+
if (head === null) return null;
71+
72+
const dummy = new ListNode();
73+
dummy.next = head;
74+
75+
let slow: ListNode | null = dummy,
76+
fast: ListNode | null = dummy;
77+
78+
// fast ํฌ์ธํŠธ ์œ„์น˜ n+1์œผ๋กœ ์ดˆ๊ธฐ ์„ธํŒ…
79+
for (let i = 0; i <= n; i++) {
80+
fast = fast.next!;
81+
}
82+
83+
while (fast !== null) {
84+
slow = slow.next!;
85+
fast = fast.next;
86+
}
87+
88+
// ๋‹ค์Œ ๋…ธ๋“œ ์‚ญ์ œ
89+
slow.next = slow.next!.next;
90+
91+
return dummy.next;
92+
}

0 commit comments

Comments
ย (0)