Skip to content

Commit 6e71f1a

Browse files
docs: 148
1 parent bd71672 commit 6e71f1a

File tree

2 files changed

+10
-73
lines changed

2 files changed

+10
-73
lines changed

explanations/148/en.md

Lines changed: 10 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
# 148. Sort List
2-
3-
**Difficulty:** Medium
4-
**Link:** https://leetcode.com/problems/sort-list/
5-
6-
## Problem Description
7-
81
Given the `head` of a linked list, return *the list after sorting it in **ascending order***.
92

103
**Example 1:**
11-
```
4+
5+
```tex
126
Input: head = [4,2,1,3]
137
Output: [1,2,3,4]
148
```
159

1610
**Example 2:**
17-
```
11+
12+
```tex
1813
Input: head = [-1,5,3,4,0]
1914
Output: [-1,0,3,4,5]
2015
```
2116

2217
**Example 3:**
23-
```
18+
19+
```tex
2420
Input: head = []
2521
Output: []
2622
```
@@ -66,17 +62,20 @@ Let's break down the solution step by step:
6662
- Set `slow.next = None` to split the list
6763

6864
**Step 4: Recursively sort halves**
65+
6966
- Sort the left half: `left = sortList(head)`
7067
- Sort the right half: `right = sortList(mid)`
7168

7269
**Step 5: Merge the sorted halves**
70+
7371
- Use a dummy node to simplify merging
7472
- Compare nodes from both lists and link them in order
7573

7674
**Example walkthrough:**
75+
7776
Let's trace through the first example:
7877

79-
```
78+
```tex
8079
head = [4,2,1,3]
8180
8281
Step 1: Find middle
@@ -101,64 +100,5 @@ result = [1,2,3,4]
101100

102101
> **Note:** Merge sort is ideal for linked lists because we can split and merge in-place without extra space. The fast/slow pointer technique efficiently finds the middle, and the merge step can be done by simply relinking nodes.
103102
104-
### Solution
105-
106-
```python
107-
# Definition for singly-linked list.
108-
# class ListNode:
109-
# def __init__(self, val=0, next=None):
110-
# self.val = val
111-
# self.next = next
112-
113-
class Solution:
114-
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
115-
# Handle base cases
116-
if not head or not head.next:
117-
return head
118-
119-
# Find the middle of the list
120-
slow = head
121-
fast = head.next
122-
123-
while fast and fast.next:
124-
slow = slow.next
125-
fast = fast.next.next
126-
127-
# Split the list
128-
mid = slow.next
129-
slow.next = None
130-
131-
# Recursively sort the two halves
132-
left = self.sortList(head)
133-
right = self.sortList(mid)
134-
135-
# Merge the sorted halves
136-
return self.merge(left, right)
137-
138-
def merge(self, left: Optional[ListNode], right: Optional[ListNode]) -> Optional[ListNode]:
139-
# Create a dummy node to simplify merging
140-
dummy = ListNode(0)
141-
current = dummy
142-
143-
# Merge the two sorted lists
144-
while left and right:
145-
if left.val <= right.val:
146-
current.next = left
147-
left = left.next
148-
else:
149-
current.next = right
150-
right = right.next
151-
current = current.next
152-
153-
# Attach remaining nodes
154-
if left:
155-
current.next = left
156-
if right:
157-
current.next = right
158-
159-
# Return the merged list (skip dummy node)
160-
return dummy.next
161-
```
162-
163103
**Time Complexity:** O(n log n) - merge sort time complexity
164104
**Space Complexity:** O(log n) - recursion stack space (not O(1) due to recursion)

solutions/148/01.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
class Solution:
99
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
10-
# Handle base cases
1110
if not head or not head.next:
1211
return head
1312

@@ -23,11 +22,9 @@ def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
2322
mid = slow.next
2423
slow.next = None
2524

26-
# Recursively sort the two halves
2725
left = self.sortList(head)
2826
right = self.sortList(mid)
2927

30-
# Merge the sorted halves
3128
return self.merge(left, right)
3229

3330
def merge(

0 commit comments

Comments
 (0)