File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Expand file tree Collapse file tree 1 file changed +32
-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+
14+ /**
15+ * list๋ ์ค๋ฆ ์ฐจ์์ผ๋ก ์ ๋ ฌ๋์ด ์๊ธฐ ๋๋ฌธ์ ๋ ๊ฐ์ ๋ฆฌ์คํธ์ head๊ฐ ๋ ์์ ๊ฐ์ด listNode์ next์ ์์๋๋ก ๋ค์ด์ค๊ฒ ํ๋ฉด ๋ฉ๋๋ค.
16+ * list ๋ ์คํ๋๊ฐ ์๋ ๊ฒฝ์ฐ์๋ ๋๋จธ์ง ๋ค๋ฅธ ํ๋์ ๋ฆฌ์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.
17+ * ๋ ํค๋ ์ค์์ ๋ ์์ ๊ฐ์ next์ ์ฌ๊ท์ ์ผ๋ก ๋ค์ ๋งํฌ๋ ๋ฆฌ์คํธ๋ฅผ ๋๊ฒจ์ฃผ๋ฉด์ ๋น๊ตํ๋ ๋ฐฉ๋ฒ์ผ๋ก ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๋ค
18+ *
19+ */
20+
21+ var mergeTwoLists = function ( list1 , list2 ) {
22+ if ( ! list1 || ! list2 ) return list1 || list2
23+
24+ if ( list1 . val < list2 . val ) {
25+ list1 . next = mergeTwoLists ( list1 . next , list2 )
26+ return list1
27+ }
28+
29+ list2 . next = mergeTwoLists ( list2 . next , list1 ) ;
30+
31+ return list2 ;
32+ } ;
You canโt perform that action at this time.
0 commit comments