File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for singly-linked list.
3+ * function ListNode(val, next) {
4+ * this.val = (val === undefined ? 0 : val);
5+ * this.next = (next === undefined ? null : next);
6+ * }
7+ */
8+
9+ /**
10+ * @param {ListNode } list1
11+ * @param {ListNode } list2
12+ * @return {ListNode }
13+ */
14+ var mergeTwoLists = function ( list1 , list2 ) {
15+ // 1) ๊ฐ์ง ์์์ (dummy)๊ณผ current ํฌ์ธํฐ ์์ฑ
16+ const dummy = new ListNode ( - 1 ) ;
17+ let current = dummy ;
18+
19+ // 2) ๋ ๋ฆฌ์คํธ ๋ชจ๋ ๋จ์ ์๋ ๋์ ๋ ์์ ๋
ธ๋๋ฅผ ์ฐ๊ฒฐ
20+ while ( list1 && list2 ) {
21+ if ( list1 . val < list2 . val ) {
22+ current . next = list1 ;
23+ list1 = list1 . next ;
24+ } else {
25+ current . next = list2 ;
26+ list2 = list2 . next ;
27+ }
28+ current = current . next ;
29+ }
30+
31+ // 3) ๋จ์ ๋
ธ๋๋ฅผ ํ ๋ฒ์ ์ด์ด๋ถ์ด๊ณ , ๊ฒฐ๊ณผ ๋ฐํ
32+ current . next = list1 || list2 ;
33+ return dummy . next ;
34+ } ;
You canโt perform that action at this time.
0 commit comments