File tree 1 file changed +37
-0
lines changed
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 시간 복잡도는 list1과 list2의 길이를 더한 것이다. 그래서 O(n + m)
2
+ // 공간 복잡도는 알고리즘에서 사용하는 새로운 메모리는 없다. 그래서 O(1)
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
+ if ( list1 === null ) return list2 ;
18
+ if ( list2 === null ) return list1 ;
19
+
20
+ let start = new ListNode ( ) ;
21
+ let current = start ;
22
+
23
+ while ( list1 !== null && list2 !== null ) {
24
+ if ( list1 . val <= list2 . val ) {
25
+ current . next = list1 ;
26
+ list1 = list1 . next ;
27
+ } else {
28
+ current . next = list2 ;
29
+ list2 = list2 . next ;
30
+ }
31
+ current = current . next ;
32
+ }
33
+
34
+ current . next = list1 || list2 ;
35
+
36
+ return start . next ;
37
+ } ;
You can’t perform that action at this time.
0 commit comments