File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-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
+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
13
+ * - 2๊ฐ์ ์ ๋ ฌ๋ ๋งํฌ๋ ๋ฆฌ์คํธ๊ฐ ์ฃผ์ด์ง๋๊น ๊ฐ ๋ฆฌ์คํธ ๊ฐ ๋น๊ตํ๋ฉด์ ์์ ๊ฐ์ ์๋ก์ด ๋งํฌ๋ ๋ฆฌ์คํธ์ ์ถ๊ฐ
14
+ * - ๋งํฌ๋ ๋ฆฌ์คํธ head์ ์ ๊ทผํด์ผํ๋๊น ๋๋ฏธ๋
ธ๋์ ํฌ์ธํฐ ๋ณ์ ๋ถ๋ฆฌํด์ ์ฌ์ฉ
15
+ * - ํฌ์ธํฐ ๋ณ์ ์ฌ์ฉํด์ ๋
ธ๋ ์ฐ๊ฒฐํ๊ธฐ
16
+ * - ๋ ๋งํฌ๋ ๋ฆฌ์คํธ๊ฐ ์๋ ๋์ ๋ฐ๋ณตํ๊ณ , ํ ์ชฝ์ด ๋๋๋ฉด ๋๋จธ์ง ๋
ธ๋๋ฅผ ๊ทธ๋๋ก ์๋ก์ด ๋งํฌ๋ ๋ฆฌ์คํธ์ ์ถ๊ฐ
17
+ *
18
+ * ์๊ฐ๋ณต์ก๋ : O(n+k)
19
+ * - n์ list1 ๊ธธ์ด, k๋ list2 ๊ธธ์ด => ๋ ๋ฆฌ์คํธ ๋ชจ๋ ๋ฐ๋ณตํ๋๊น O(n+k)
20
+ *
21
+ * ๊ณต๊ฐ๋ณต์ก๋ : O(1)
22
+ * - ๊ธฐ์กด ๋
ธ๋ ์ฐ๊ฒฐํด์ ์ฌ์ฌ์ฉํ๋๊น O(1)
23
+ */
24
+
25
+ function mergeTwoLists (
26
+ list1 : ListNode | null ,
27
+ list2 : ListNode | null
28
+ ) : ListNode | null {
29
+ const dummyNode = new ListNode ( ) ;
30
+ let current = dummyNode ;
31
+
32
+ while ( list1 !== null && list2 !== null ) {
33
+ if ( list1 . val <= list2 . val ) {
34
+ current . next = list1 ;
35
+ list1 = list1 . next ;
36
+ current = current . next ;
37
+ } else {
38
+ current . next = list2 ;
39
+ list2 = list2 . next ;
40
+ current = current . next ;
41
+ }
42
+ }
43
+ current . next = list1 !== null ? list1 : list2 ;
44
+
45
+ return dummyNode . next ;
46
+ }
You canโt perform that action at this time.
0 commit comments