File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Definition for singly-linked list.
2
+ class ListNode {
3
+ val : number
4
+ next : ListNode | null
5
+ constructor ( val ?: number , next ?: ListNode | null ) {
6
+ this . val = ( val === undefined ? 0 : val )
7
+ this . next = ( next === undefined ? null : next )
8
+ }
9
+ }
10
+
11
+ function reverseList ( head : ListNode | null ) : ListNode | null {
12
+ let prev : ListNode | null = null ;
13
+ let current = head ;
14
+ let next = null ;
15
+
16
+ while ( current !== null ) {
17
+ const next = current . next ; // 1. ๋ค์ ๋
ธ๋๋ฅผ ๊ธฐ์ตํด๋๊ณ
18
+ current . next = prev ; // 2. ํ์ฌ ๋
ธ๋๊ฐ ์ด์ ๋
ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋๋ก
19
+ prev = current ; // 3. ์ด์ ๋
ธ๋๋ฅผ ์ง๊ธ ๋
ธ๋๋ก ์
๋ฐ์ดํธ
20
+ current = next ; // 4. ํ์ฌ ๋
ธ๋๋ฅผ ๋ค์ ๋
ธ๋๋ก ์ด๋
21
+ }
22
+
23
+
24
+ return prev ;
25
+ } ;
You canโt perform that action at this time.
0 commit comments