Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.0148 #3904

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 93 additions & 87 deletions solution/0100-0199/0148.Sort List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:归并排序

我们可以用归并排序的思想来解决。

首先,我们利用快慢指针找到链表的中点,将链表从中点处断开,形成两个独立的子链表 $\textit{l1}$ 和 $\textit{l2}$。

然后,我们递归地对 $\textit{l1}$ 和 $\textit{l2}$ 进行排序,最后将 $\textit{l1}$ 和 $\textit{l2}$ 合并为一个有序链表。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是链表的长度。

<!-- tabs:start -->

Expand All @@ -80,26 +88,27 @@ tags:
# self.val = val
# self.next = next
class Solution:
def sortList(self, head: ListNode) -> ListNode:
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head is None or head.next is None:
return head
slow, fast = head, head.next
while fast and fast.next:
slow, fast = slow.next, fast.next.next
t = slow.next
slow = slow.next
fast = fast.next.next
l1, l2 = head, slow.next
slow.next = None
l1, l2 = self.sortList(head), self.sortList(t)
l1, l2 = self.sortList(l1), self.sortList(l2)
dummy = ListNode()
cur = dummy
tail = dummy
while l1 and l2:
if l1.val <= l2.val:
cur.next = l1
tail.next = l1
l1 = l1.next
else:
cur.next = l2
tail.next = l2
l2 = l2.next
cur = cur.next
cur.next = l1 or l2
tail = tail.next
tail.next = l1 or l2
return dummy.next
```

Expand All @@ -126,23 +135,23 @@ class Solution {
slow = slow.next;
fast = fast.next.next;
}
ListNode t = slow.next;
ListNode l1 = head, l2 = slow.next;
slow.next = null;
ListNode l1 = sortList(head);
ListNode l2 = sortList(t);
l1 = sortList(l1);
l2 = sortList(l2);
ListNode dummy = new ListNode();
ListNode cur = dummy;
ListNode tail = dummy;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
cur.next = l1;
tail.next = l1;
l1 = l1.next;
} else {
cur.next = l2;
tail.next = l2;
l2 = l2.next;
}
cur = cur.next;
tail = tail.next;
}
cur.next = l1 == null ? l2 : l1;
tail.next = l1 != null ? l1 : l2;
return dummy.next;
}
}
Expand All @@ -164,30 +173,33 @@ class Solution {
class Solution {
public:
ListNode* sortList(ListNode* head) {
if (!head || !head->next) return head;
auto* slow = head;
auto* fast = head->next;
if (!head || !head->next) {
return head;
}
ListNode* slow = head;
ListNode* fast = head->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
auto* t = slow->next;
ListNode* l1 = head;
ListNode* l2 = slow->next;
slow->next = nullptr;
auto* l1 = sortList(head);
auto* l2 = sortList(t);
auto* dummy = new ListNode();
auto* cur = dummy;
l1 = sortList(l1);
l2 = sortList(l2);
ListNode* dummy = new ListNode();
ListNode* tail = dummy;
while (l1 && l2) {
if (l1->val <= l2->val) {
cur->next = l1;
tail->next = l1;
l1 = l1->next;
} else {
cur->next = l2;
tail->next = l2;
l2 = l2->next;
}
cur = cur->next;
tail = tail->next;
}
cur->next = l1 ? l1 : l2;
tail->next = l1 ? l1 : l2;
return dummy->next;
}
};
Expand All @@ -211,25 +223,27 @@ func sortList(head *ListNode) *ListNode {
for fast != nil && fast.Next != nil {
slow, fast = slow.Next, fast.Next.Next
}
t := slow.Next
l1 := head
l2 := slow.Next
slow.Next = nil
l1, l2 := sortList(head), sortList(t)
l1 = sortList(l1)
l2 = sortList(l2)
dummy := &ListNode{}
cur := dummy
tail := dummy
for l1 != nil && l2 != nil {
if l1.Val <= l2.Val {
cur.Next = l1
tail.Next = l1
l1 = l1.Next
} else {
cur.Next = l2
tail.Next = l2
l2 = l2.Next
}
cur = cur.Next
tail = tail.Next
}
if l1 != nil {
cur.Next = l1
tail.Next = l1
} else {
cur.Next = l2
tail.Next = l2
}
return dummy.Next
}
Expand All @@ -251,32 +265,31 @@ func sortList(head *ListNode) *ListNode {
*/

function sortList(head: ListNode | null): ListNode | null {
if (head == null || head.next == null) return head;
// 快慢指针定位中点
let slow: ListNode = head,
fast: ListNode = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
if (head === null || head.next === null) {
return head;
}
let [slow, fast] = [head, head.next];
while (fast !== null && fast.next !== null) {
slow = slow.next!;
fast = fast.next.next;
}
// 归并排序
let mid: ListNode = slow.next;
let [l1, l2] = [head, slow.next];
slow.next = null;
let l1: ListNode = sortList(head);
let l2: ListNode = sortList(mid);
let dummy: ListNode = new ListNode();
let cur: ListNode = dummy;
while (l1 != null && l2 != null) {
l1 = sortList(l1);
l2 = sortList(l2);
const dummy = new ListNode();
let tail = dummy;
while (l1 !== null && l2 !== null) {
if (l1.val <= l2.val) {
cur.next = l1;
tail.next = l1;
l1 = l1.next;
} else {
cur.next = l2;
tail.next = l2;
l2 = l2.next;
}
cur = cur.next;
tail = tail.next;
}
cur.next = l1 == null ? l2 : l1;
tail.next = l1 ?? l2;
return dummy.next;
}
```
Expand Down Expand Up @@ -357,32 +370,31 @@ impl Solution {
* @return {ListNode}
*/
var sortList = function (head) {
if (!head || !head.next) {
if (head === null || head.next === null) {
return head;
}
let slow = head;
let fast = head.next;
while (fast && fast.next) {
let [slow, fast] = [head, head.next];
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
}
let t = slow.next;
let [l1, l2] = [head, slow.next];
slow.next = null;
let l1 = sortList(head);
let l2 = sortList(t);
l1 = sortList(l1);
l2 = sortList(l2);
const dummy = new ListNode();
let cur = dummy;
while (l1 && l2) {
let tail = dummy;
while (l1 !== null && l2 !== null) {
if (l1.val <= l2.val) {
cur.next = l1;
tail.next = l1;
l1 = l1.next;
} else {
cur.next = l2;
tail.next = l2;
l2 = l2.next;
}
cur = cur.next;
tail = tail.next;
}
cur.next = l1 || l2;
tail.next = l1 ?? l2;
return dummy.next;
};
```
Expand All @@ -403,37 +415,31 @@ var sortList = function (head) {
*/
public class Solution {
public ListNode SortList(ListNode head) {
if (head == null || head.next == null)
{
if (head == null || head.next == null) {
return head;
}
ListNode slow = head, fast = head.next;
while (fast != null && fast.next != null)
{
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode t = slow.next;
ListNode l1 = head, l2 = slow.next;
slow.next = null;
ListNode l1 = SortList(head);
ListNode l2 = SortList(t);
l1 = SortList(l1);
l2 = SortList(l2);
ListNode dummy = new ListNode();
ListNode cur = dummy;
while (l1 != null && l2 != null)
{
if (l1.val <= l2.val)
{
cur.next = l1;
ListNode tail = dummy;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
tail.next = l1;
l1 = l1.next;
}
else
{
cur.next = l2;
} else {
tail.next = l2;
l2 = l2.next;
}
cur = cur.next;
tail = tail.next;
}
cur.next = l1 == null ? l2 : l1;
tail.next = l1 != null ? l1 : l2;
return dummy.next;
}
}
Expand Down
Loading
Loading