Skip to content

Commit ece6e47

Browse files
committed
feat(soobing): week7 > reverse-linked-list
1 parent baf6fe2 commit ece6e47

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* ๋ฌธ์ œ ์„ค๋ช…
3+
* - ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋’ค์ง‘๋Š” ๋ฌธ์ œ
4+
*
5+
* ์•„์ด๋””์–ด
6+
* - ๋…ธ๋“œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๋ฏธ๋ฆฌ ๊ธฐ์–ตํ•ด๋‘๊ณ , ํ™”์‚ดํ‘œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ๋Œ๋ฆฐ ํ›„ ํ•œ์นธ์”ฉ ์ด๋™ํ•œ๋‹ค.
7+
* - prev, head(current) ๋‘ ๊ฐ€์ง€ ํฌ์ธํ„ฐ๊ฐ€ ํ•„์š”
8+
*/
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* class ListNode {
13+
* val: number
14+
* next: ListNode | null
15+
* constructor(val?: number, next?: ListNode | null) {
16+
* this.val = (val===undefined ? 0 : val)
17+
* this.next = (next===undefined ? null : next)
18+
* }
19+
* }
20+
*/
21+
22+
interface ListNode {
23+
val: number;
24+
next: ListNode | null;
25+
}
26+
27+
function reverseList(head: ListNode | null): ListNode | null {
28+
let prev: ListNode | null = null;
29+
30+
while (head) {
31+
const next = head.next;
32+
head.next = prev;
33+
prev = head;
34+
head = next;
35+
}
36+
37+
return prev;
38+
}

0 commit comments

Comments
ย (0)