File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ // 시간복잡도: O(m + n)
2+ // 공간복잡도: O(m + n)
3+
4+ /**
5+ * Definition for singly-linked list.
6+ * function ListNode(val, next) {
7+ * this.val = (val===undefined ? 0 : val)
8+ * this.next = (next===undefined ? null : next)
9+ * }
10+ */
11+ /**
12+ * @param {ListNode } list1
13+ * @param {ListNode } list2
14+ * @return {ListNode }
15+ */
16+ var mergeTwoLists = function ( list1 , list2 ) {
17+ let res = new ListNode ( )
18+ let resCopy = res
19+
20+ while ( list1 && list2 ) {
21+ if ( list1 . val < list2 . val ) {
22+ res . next = new ListNode ( list1 . val ) ;
23+ list1 = list1 . next ;
24+ } else {
25+ res . next = new ListNode ( list2 . val ) ;
26+ list2 = list2 . next ;
27+ }
28+
29+ res = res . next
30+ }
31+
32+ if ( list1 ) res . next = list1 ;
33+ if ( list2 ) res . next = list2
34+
35+ return resCopy . next
36+ } ;
37+
You can’t perform that action at this time.
0 commit comments