File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ *
12
+ * @link https://leetcode.com/problems/reverse-linked-list/
13
+ *
14
+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
15
+ * - ๋ฆฌ์คํธ ์ํํ๋ฉด์ ์๋ก์ด ๋
ธ๋๋ฅผ ์์ฑํ๊ณ ๊ธฐ์กด reversed ๋ฆฌ์คํธ์ head๋ฅผ ์ฐ๊ฒฐ
16
+ *
17
+ * ์๊ฐ๋ณต์ก๋ : O(n)
18
+ * - ๋ฆฌ์คํธ ๋
ธ๋ 1ํ ์ํํ๋๊น
19
+ *
20
+ * ๊ณต๊ฐ๋ณต์ก๋ : O(n)
21
+ * - reversed ๋ ๋งํฌ๋ ๋ฆฌ์คํธ ์๋ก ๋ง๋๋๊น
22
+ */
23
+
24
+ function reverseList ( head : ListNode | null ) : ListNode | null {
25
+ if ( head === null ) return head ;
26
+
27
+ let headNode : ListNode | null = null ;
28
+ let currentNode : ListNode | null = head ;
29
+
30
+ while ( currentNode !== null ) {
31
+ const newNode = new ListNode ( currentNode . val , headNode ) ;
32
+ headNode = newNode ;
33
+ currentNode = currentNode . next ;
34
+ }
35
+
36
+ return headNode ;
37
+ }
You canโt perform that action at this time.
0 commit comments