1
- using LeetCode . SharedUtils ;
2
-
3
-
4
- namespace LeetCode . Hard
1
+ namespace LeetCode . HardProblems
5
2
{
6
- //https://leetcode.com/problems/merge-k-sorted-lists/submissions/
7
- class Merge_k_Sorted_Lists
3
+ class MergeKSortedLists
8
4
{
9
5
public ListNode MergeKLists ( ListNode [ ] lists )
10
6
{
11
- if ( lists != null && lists . Length > 0 )
12
- {
13
- if ( lists . Length == 1 )
14
- {
15
- return lists [ 0 ] ;
16
- }
17
- else
18
- {
19
- var currNode = lists [ 0 ] ;
20
-
21
- for ( int i = 1 ; i < lists . Length ; i ++ )
22
- {
23
- currNode = Merge ( currNode , lists [ i ] ) ;
24
- }
7
+ // Handle edge cases
8
+ if ( lists == null || lists . Length == 0 ) return null ;
25
9
26
- return currNode ;
27
- }
28
- }
29
- else
30
- {
31
- return null ;
32
- }
10
+ // Start the divide-and-conquer process
11
+ return MergeDivideAndConquer ( lists , 0 , lists . Length - 1 ) ;
33
12
}
34
13
35
- private ListNode Merge ( ListNode node1 , ListNode node2 )
14
+ private ListNode MergeDivideAndConquer ( ListNode [ ] lists , int left , int right )
36
15
{
37
- ListNode returnValue = new ListNode ( 0 ) ;
38
- var curr = returnValue ;
16
+ // Base case: only one list in the current range
17
+ if ( left == right ) return lists [ left ] ;
39
18
40
- while ( node1 != null && node2 != null )
41
- {
42
- if ( node1 != null && node2 != null )
43
- {
44
- if ( node1 . val < node2 . val )
45
- {
46
- curr . next = node1 ;
47
- node1 = node1 . next ;
48
- }
49
- else
50
- {
51
- curr . next = node2 ;
52
- node2 = node2 . next ;
53
- }
54
- }
19
+ // Divide the problem into two halves
20
+ int mid = left + ( right - left ) / 2 ;
21
+ ListNode leftList = MergeDivideAndConquer ( lists , left , mid ) ;
22
+ ListNode rightList = MergeDivideAndConquer ( lists , mid + 1 , right ) ;
55
23
56
- curr = curr . next ;
57
- }
58
-
59
- if ( node1 == null )
60
- {
61
- curr . next = node2 ;
62
- }
63
-
64
- if ( node2 == null )
65
- {
66
- curr . next = node1 ;
67
- }
68
-
69
- return returnValue . next ;
24
+ // Merge the two halves
25
+ return MergeTwoLists ( leftList , rightList ) ;
70
26
}
71
27
72
- public ListNode MergeKLists2 ( ListNode [ ] lists )
28
+ private ListNode MergeTwoLists ( ListNode l1 , ListNode l2 )
73
29
{
74
- if ( lists != null || lists . Count ( ) > 0 )
30
+ // Create a dummy head to simplify edge cases
31
+ ListNode dummy = new ListNode ( ) ;
32
+ ListNode current = dummy ;
33
+
34
+ // Compare and link nodes in sorted order
35
+ while ( l1 != null && l2 != null )
75
36
{
76
- List < int > vals = new List < int > ( ) ;
77
- foreach ( var item in lists )
37
+ if ( l1 . val <= l2 . val )
78
38
{
79
- vals = Read ( item , vals ) ;
39
+ current . next = l1 ;
40
+ l1 = l1 . next ;
80
41
}
81
-
82
- var orderedList = vals . OrderBy ( x => x ) . ToArray ( ) ;
83
-
84
- ListNode currNode = default , prevNode = default ;
85
- for ( int i = orderedList . Length - 1 ; i >= 0 ; i -- )
42
+ else
86
43
{
87
- prevNode = currNode ;
88
- currNode = new ListNode ( orderedList [ i ] , prevNode ) ;
44
+ current . next = l2 ;
45
+ l2 = l2 . next ;
89
46
}
90
47
91
- return currNode ;
92
- }
93
- else
94
- {
95
- return default ;
48
+ current = current . next ;
96
49
}
50
+
51
+ // Attach remaining nodes (if any)
52
+ current . next = ( l1 != null ) ? l1 : l2 ;
53
+
54
+ return dummy . next ;
97
55
}
98
56
99
- private List < int > Read ( ListNode node , List < int > val )
100
- {
101
- if ( node != null )
102
- {
103
- val . Add ( node . val ) ;
104
- val = Read ( node . next , val ) ;
105
- }
106
57
107
- return val ;
58
+ [ Test ( Description = "https://leetcode.com/problems/merge-k-sorted-lists/" ) ]
59
+ [ Category ( "Hard" ) ]
60
+ [ Category ( "LeetCode" ) ]
61
+ [ Category ( "Merge K Sorted Lists" ) ]
62
+ [ TestCaseSource ( nameof ( Input ) ) ]
63
+ public void Test1 ( ( int [ ] Output , int [ ] [ ] Input ) item )
64
+ {
65
+ var response = MergeKLists ( item . Input . Select ( x => x . ToListNode ( ) ) . ToArray ( ) ) ;
66
+ Assert . That ( response . ToArray ( ) , Is . EqualTo ( item . Output ) ) ;
108
67
}
68
+
69
+ public static IEnumerable < ( int [ ] Output , int [ ] [ ] Input ) > Input =>
70
+ new List < ( int [ ] Output , int [ ] [ ] Input ) > ( )
71
+ {
72
+ ( [ 1 , 1 , 2 , 3 , 4 , 4 , 5 , 6 ] , ( [ [ 1 , 4 , 5 ] , [ 1 , 3 , 4 ] , [ 2 , 6 ] ] ) ) ,
73
+ } ;
109
74
}
110
75
}
0 commit comments