Skip to content

Commit 633908e

Browse files
committed
https://leetcode.com/problems/sort-list/
1 parent c6ecdd4 commit 633908e

File tree

3 files changed

+93
-6
lines changed

3 files changed

+93
-6
lines changed

LeetCodeSolutions/DivideAndConquer/ConvertSortedArrayToBinarySearchTree.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using LeetCodeSolutions.Utils;
2-
using NUnit.Framework;
31

42
namespace LeetCodeSolutions.DivideAndConquer;
53

@@ -30,12 +28,12 @@ private TreeNode BuildTree(int[] nums, int i, int j)
3028
return node;
3129
}
3230

33-
[Test(Description = "https://leetcode.com/problems/count-complete-tree-nodes/")]
31+
[Test(Description = "https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/")]
3432
[Category("Easy")]
3533
[Category("LeetCode")]
36-
[Category("Count Complete Tree Nodes")]
34+
[Category("Convert Sorted Array To Binary Search Tree")]
3735
[TestCaseSource(nameof(Input))]
38-
[Category("BinaryTreeGeneral")]
36+
[Category("DivideAndConquer")]
3937
[Category("TopInterview")]
4038
public void Test1((int?[] Output, int[] Input) item)
4139
{
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
namespace LeetCodeSolutions.DivideAndConquer
2+
{
3+
class SortListSolution
4+
{
5+
public ListNode SortList(ListNode head)
6+
{
7+
// Base case: empty list or single node is already sorted
8+
if (head == null || head.next == null)
9+
{
10+
return head;
11+
}
12+
13+
// Split the list into two halves
14+
ListNode middle = GetMiddle(head);
15+
ListNode right = middle.next;
16+
middle.next = null; // Disconnect the two halves
17+
18+
// Recursively sort both halves
19+
ListNode left = SortList(head);
20+
right = SortList(right);
21+
22+
// Merge the sorted halves
23+
return Merge(left, right);
24+
}
25+
26+
// Find the middle of the linked list using slow/fast pointers
27+
private ListNode GetMiddle(ListNode head)
28+
{
29+
ListNode slow = head;
30+
ListNode fast = head.next;
31+
32+
while (fast != null && fast.next != null)
33+
{
34+
slow = slow.next;
35+
fast = fast.next.next;
36+
}
37+
38+
return slow;
39+
}
40+
41+
// Merge two sorted linked lists
42+
private ListNode Merge(ListNode left, ListNode right)
43+
{
44+
ListNode dummy = new ListNode();
45+
ListNode current = dummy;
46+
47+
// Compare nodes and link them in sorted order
48+
while (left != null && right != null)
49+
{
50+
if (left.val < right.val)
51+
{
52+
current.next = left;
53+
left = left.next;
54+
}
55+
else
56+
{
57+
current.next = right;
58+
right = right.next;
59+
}
60+
61+
current = current.next;
62+
}
63+
64+
// Attach remaining nodes (if any)
65+
current.next = (left != null) ? left : right;
66+
67+
return dummy.next;
68+
}
69+
70+
[Test(Description = "https://leetcode.com/problems/sort-list/")]
71+
[Category("Medium")]
72+
[Category("LeetCode")]
73+
[Category("Sort List")]
74+
[TestCaseSource(nameof(Input))]
75+
[Category("DivideAndConquer")]
76+
[Category("TopInterview")]
77+
public void Test1((int?[] Output, int[] Input) item)
78+
{
79+
var response = SortList(item.Input.ToListNode());
80+
Assert.That(response.ToArray(), Is.EqualTo(item.Output));
81+
}
82+
83+
public static IEnumerable<(int[] Output, int[] Input)> Input =>
84+
new List<(int[] Output, int[] Input)>()
85+
{
86+
([-1, 0, 3, 4, 5], ( [-1, 5, 3, 4, 0])),
87+
};
88+
}
89+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ The Top Interview 150 collection on Leetcode is a curated set of 150 interview q
167167
| 107 | Word Search | Medium | |
168168
| <br> Divide &amp; Conquer<br> | | | |
169169
| 108 | Convert Sorted Array to Binary Search Tree | Easy ||
170-
| 109 | Sort List | Medium | |
170+
| 109 | Sort List | Medium | |
171171
| 110 | Construct Quad Tree | Medium | |
172172
| 111 | Merge k Sorted Lists | Hard | |
173173
| <br> Kadane&#39;s Algorithm<br> | | | |

0 commit comments

Comments
 (0)