File tree Expand file tree Collapse file tree 1 file changed +22
-2
lines changed Expand file tree Collapse file tree 1 file changed +22
-2
lines changed Original file line number Diff line number Diff line change 1111 */
1212
1313function mergeTwoLists ( list1 : ListNode | null , list2 : ListNode | null ) : ListNode | null {
14-
15- } ;
14+ // ๋๋ฏธ ํค๋ ๋
ธ๋๋ฅผ ์์ฑํ์ฌ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ์์์ ์ผ๋ก ์ฌ์ฉ
15+ const dummy = new ListNode ( 0 ) ;
16+ let current = dummy ;
17+
18+ // ๋ ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ฉด์ ๋ ์์ ๊ฐ์ ๊ฐ์ง ๋
ธ๋๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ์ฐ๊ฒฐ
19+ while ( list1 !== null && list2 !== null ) {
20+ if ( list1 . val <= list2 . val ) {
21+ current . next = list1 ;
22+ list1 = list1 . next ;
23+ } else {
24+ current . next = list2 ;
25+ list2 = list2 . next ;
26+ }
27+ current = current . next ;
28+ }
29+
30+ // ๋จ์ ๋
ธ๋๋ค์ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ์ฐ๊ฒฐ
31+ current . next = list1 === null ? list2 : list1 ;
32+
33+ // ๋๋ฏธ ๋
ธ๋์ ๋ค์ ๋
ธ๋๊ฐ ์ค์ ๋ณํฉ๋ ๋ฆฌ์คํธ์ ํค๋
34+ return dummy . next ;
35+ }
You canโt perform that action at this time.
0 commit comments