3
3
4
4
namespace LeetCode . MediumProblems
5
5
{
6
- class Sort_List
6
+ class SortListSolution
7
7
{
8
- /// <summary>
9
- /// https://leetcode.com/problems/sort-list/
10
- /// </summary>
11
- /// <param name="head"></param>
12
- /// <returns></returns>
13
8
public ListNode SortList ( ListNode head )
14
9
{
15
- List < int > nums = new List < int > ( ) ;
16
- Read ( head , nums ) ;
10
+ // Base case: empty list or single node is already sorted
11
+ if ( head == null || head . next == null )
12
+ {
13
+ return head ;
14
+ }
15
+
16
+ // Split the list into two halves
17
+ ListNode middle = GetMiddle ( head ) ;
18
+ ListNode right = middle . next ;
19
+ middle . next = null ; // Disconnect the two halves
20
+
21
+ // Recursively sort both halves
22
+ ListNode left = SortList ( head ) ;
23
+ right = SortList ( right ) ;
17
24
18
- nums = nums . OrderByDescending ( x => x ) . ToList ( ) ;
25
+ // Merge the sorted halves
26
+ return Merge ( left , right ) ;
27
+ }
28
+
29
+ // Find the middle of the linked list using slow/fast pointers
30
+ private ListNode GetMiddle ( ListNode head )
31
+ {
32
+ ListNode slow = head ;
33
+ ListNode fast = head . next ;
19
34
20
- ListNode currNode = default , prevNode = new ListNode ( ) ;
21
- int i = 0 ;
22
- while ( i < nums . Count )
35
+ while ( fast != null && fast . next != null )
23
36
{
24
- prevNode = currNode ;
25
- currNode = new ListNode ( nums [ i ] , prevNode ) ;
26
- i ++ ;
37
+ slow = slow . next ;
38
+ fast = fast . next . next ;
27
39
}
28
40
29
- return currNode ;
41
+ return slow ;
30
42
}
31
43
32
- private void Read ( ListNode node , List < int > result )
44
+ // Merge two sorted linked lists
45
+ private ListNode Merge ( ListNode left , ListNode right )
33
46
{
34
- if ( node != null )
47
+ ListNode dummy = new ListNode ( ) ;
48
+ ListNode current = dummy ;
49
+
50
+ // Compare nodes and link them in sorted order
51
+ while ( left != null && right != null )
35
52
{
36
- result . Add ( node . val ) ;
37
- Read ( node . next , result ) ;
53
+ if ( left . val < right . val )
54
+ {
55
+ current . next = left ;
56
+ left = left . next ;
57
+ }
58
+ else
59
+ {
60
+ current . next = right ;
61
+ right = right . next ;
62
+ }
63
+
64
+ current = current . next ;
38
65
}
66
+
67
+ // Attach remaining nodes (if any)
68
+ current . next = ( left != null ) ? left : right ;
69
+
70
+ return dummy . next ;
71
+ }
72
+
73
+ [ Test ( Description = "https://leetcode.com/problems/sort-list/" ) ]
74
+ [ Category ( "Medium" ) ]
75
+ [ Category ( "LeetCode" ) ]
76
+ [ Category ( "Sort List" ) ]
77
+ [ TestCaseSource ( nameof ( Input ) ) ]
78
+ public void Test1 ( ( int ? [ ] Output , int [ ] Input ) item )
79
+ {
80
+ var response = SortList ( item . Input . ToListNode ( ) ) ;
81
+ Assert . That ( response . ToArray ( ) , Is . EqualTo ( item . Output ) ) ;
39
82
}
83
+
84
+ public static IEnumerable < ( int [ ] Output , int [ ] Input ) > Input =>
85
+ new List < ( int [ ] Output , int [ ] Input ) > ( )
86
+ {
87
+ ( [ - 1 , 0 , 3 , 4 , 5 ] , ( [ - 1 , 5 , 3 , 4 , 0 ] ) ) ,
88
+ } ;
40
89
}
41
- }
90
+ }
0 commit comments