File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ */
4
+ function ListNode ( val , next ) {
5
+ this . val = ( val === undefined ? 0 : val )
6
+ this . next = ( next === undefined ? null : next )
7
+ }
8
+
9
+ /**
10
+ * 주어진 두 연결 리스트를 하나의 정렬 리스트로 병합해 반환하는 함수
11
+ * @param {ListNode } list1
12
+ * @param {ListNode } list2
13
+ * @return {ListNode }
14
+ */
15
+ const mergeTwoLists = function ( list1 , list2 ) {
16
+ let head = new ListNode ( - 1 , null ) ;
17
+ let mergedList = head ;
18
+
19
+ let node1 = list1 ;
20
+ let node2 = list2 ;
21
+
22
+ while ( node1 && node2 ) {
23
+ if ( node1 . val >= node2 . val ) {
24
+ mergedList . next = node2 ;
25
+ node2 = node2 . next ;
26
+ } else if ( node1 . val < node2 . val ) {
27
+ mergedList . next = node1 ;
28
+ node1 = node1 . next ;
29
+ }
30
+
31
+ mergedList = mergedList . next ;
32
+ }
33
+
34
+ mergedList . next = node1 ?? node2 ;
35
+
36
+ return head . next ;
37
+ } ;
38
+
39
+ // 시간복잡도: O(n)
40
+ // 공간복잡도: O(n)
You can’t perform that action at this time.
0 commit comments