Skip to content

Commit d314a42

Browse files
committed
https://leetcode.com/problems/sort-list/
1 parent d236b68 commit d314a42

File tree

2 files changed

+72
-23
lines changed

2 files changed

+72
-23
lines changed

EasyProblems/ConvertSortedArrayToBinarySearchTree.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ private TreeNode BuildTree(int[] nums, int i, int j)
2929
return node;
3030
}
3131

32-
[Test(Description = "https://leetcode.com/problems/count-complete-tree-nodes/")]
32+
[Test(Description = "https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/")]
3333
[Category("Easy")]
3434
[Category("LeetCode")]
35-
[Category("Count Complete Tree Nodes")]
35+
[Category("Convert Sorted Array To Binary Search Tree")]
3636
[TestCaseSource(nameof(Input))]
3737
public void Test1((int?[] Output, int[] Input) item)
3838
{

MediumProblems/Sort List.cs

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,88 @@
33

44
namespace LeetCode.MediumProblems
55
{
6-
class Sort_List
6+
class SortListSolution
77
{
8-
/// <summary>
9-
/// https://leetcode.com/problems/sort-list/
10-
/// </summary>
11-
/// <param name="head"></param>
12-
/// <returns></returns>
138
public ListNode SortList(ListNode head)
149
{
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);
1724

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;
1934

20-
ListNode currNode = default, prevNode = new ListNode();
21-
int i = 0;
22-
while (i < nums.Count)
35+
while (fast != null && fast.next != null)
2336
{
24-
prevNode = currNode;
25-
currNode = new ListNode(nums[i], prevNode);
26-
i++;
37+
slow = slow.next;
38+
fast = fast.next.next;
2739
}
2840

29-
return currNode;
41+
return slow;
3042
}
3143

32-
private void Read(ListNode node, List<int> result)
44+
// Merge two sorted linked lists
45+
private ListNode Merge(ListNode left, ListNode right)
3346
{
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)
3552
{
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;
3865
}
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));
3982
}
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+
};
4089
}
41-
}
90+
}

0 commit comments

Comments
 (0)