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+ * @param {ListNode } list1
10+ * @param {ListNode } list2
11+ * @return {ListNode }
12+ */
13+ var mergeTwoLists = function ( list1 , list2 ) {
14+ // ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ์์์ ์ ์ํ ๋๋ฏธ ๋
ธ๋
15+ let dummy = new ListNode ( - 1 ) ;
16+ let current = dummy ;
17+
18+ // ๋ ๋ค null์ด ์๋ ๋๊น์ง ๋ฐ๋ณต
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 ? list1 : list2 ;
32+
33+ return dummy . next ; // dummy ๋ค์์ด ์ง์ง head
34+ } ;
You canโt perform that action at this time.
0 commit comments