|
| 1 | +--- |
| 2 | +id: sort-list |
| 3 | +title: Sort List |
| 4 | +sidebar_label: 0148- Sort List |
| 5 | +tags: |
| 6 | + - DSA |
| 7 | + - Leetcode |
| 8 | + - Linked List |
| 9 | + |
| 10 | +description: "This is a solution to the Sort List on LeetCode." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Statement |
| 14 | + |
| 15 | +Given the head of a linked list, return the list after sorting it in ascending order. |
| 16 | + |
| 17 | +**Example 1:** |
| 18 | + |
| 19 | +``` |
| 20 | +Input: head = [4,2,1,3] |
| 21 | +Output: [1,2,3,4] |
| 22 | +``` |
| 23 | + |
| 24 | +**Example 2:** |
| 25 | + |
| 26 | +``` |
| 27 | +Input: head = [-1,5,3,4,0] |
| 28 | +Output: [-1,0,3,4,5] |
| 29 | +``` |
| 30 | + |
| 31 | +**Example 3:** |
| 32 | + |
| 33 | +``` |
| 34 | +Input: head = [] |
| 35 | +Output: [] |
| 36 | +``` |
| 37 | + |
| 38 | +### Constraints: |
| 39 | + |
| 40 | +- The number of nodes in the list is in the range $[0, 5 * 10^4].$ |
| 41 | +- $-10^5 <= Node.val <= 10^5$ |
| 42 | + |
| 43 | +## Algorithm |
| 44 | + |
| 45 | +The algorithm uses the merge sort technique to sort a singly linked list. The steps involved are as follows: |
| 46 | + |
| 47 | +1. **Base Case**: |
| 48 | + |
| 49 | + - If the list is empty or contains only one element, it is already sorted. Return the head. |
| 50 | + |
| 51 | +2. **Splitting the List**: |
| 52 | + |
| 53 | + - Use two pointers, `slow` and `fast`, to find the middle of the list. |
| 54 | + - Move `slow` one step at a time and `fast` two steps at a time. |
| 55 | + - When `fast` reaches the end of the list, `slow` will be at the middle. |
| 56 | + - Split the list into two halves at the middle. |
| 57 | + |
| 58 | +3. **Recursive Sorting**: |
| 59 | + |
| 60 | + - Recursively sort the two halves. |
| 61 | + |
| 62 | +4. **Merging**: |
| 63 | + - Merge the two sorted halves into a single sorted list. |
| 64 | + |
| 65 | +## C++ Implementation |
| 66 | + |
| 67 | +```cpp |
| 68 | +class Solution { |
| 69 | +public: |
| 70 | + ListNode* sortList(ListNode* head) { |
| 71 | + if (head == NULL || head->next == NULL) |
| 72 | + return head; |
| 73 | + |
| 74 | + ListNode *temp = NULL; |
| 75 | + ListNode *slow = head; |
| 76 | + ListNode *fast = head; |
| 77 | + |
| 78 | + while (fast != NULL && fast->next != NULL) { |
| 79 | + temp = slow; |
| 80 | + slow = slow->next; |
| 81 | + fast = fast->next->next; |
| 82 | + } |
| 83 | + temp->next = NULL; |
| 84 | + |
| 85 | + ListNode* l1 = sortList(head); |
| 86 | + ListNode* l2 = sortList(slow); |
| 87 | + |
| 88 | + return merge(l1, l2); |
| 89 | + } |
| 90 | + |
| 91 | + ListNode* merge(ListNode *l1, ListNode *l2) { |
| 92 | + ListNode *ptr = new ListNode(0); |
| 93 | + ListNode *curr = ptr; |
| 94 | + |
| 95 | + while (l1 != NULL && l2 != NULL) { |
| 96 | + if (l1->val <= l2->val) { |
| 97 | + curr->next = l1; |
| 98 | + l1 = l1->next; |
| 99 | + } else { |
| 100 | + curr->next = l2; |
| 101 | + l2 = l2->next; |
| 102 | + } |
| 103 | + curr = curr->next; |
| 104 | + } |
| 105 | + if (l1 != NULL) { |
| 106 | + curr->next = l1; |
| 107 | + } |
| 108 | + if (l2 != NULL) { |
| 109 | + curr->next = l2; |
| 110 | + } |
| 111 | + return ptr->next; |
| 112 | + } |
| 113 | +}; |
| 114 | +``` |
| 115 | +
|
| 116 | +## Python Implementation |
| 117 | +
|
| 118 | +```python |
| 119 | +class ListNode: |
| 120 | + def __init__(self, x=0, next=None): |
| 121 | + self.val = x |
| 122 | + self.next = next |
| 123 | +
|
| 124 | +class Solution: |
| 125 | + def sortList(self, head: ListNode) -> ListNode: |
| 126 | + if not head or not head.next: |
| 127 | + return head |
| 128 | +
|
| 129 | + # Find the middle point |
| 130 | + slow, fast = head, head |
| 131 | + prev = None |
| 132 | + while fast and fast.next: |
| 133 | + prev = slow |
| 134 | + slow = slow.next |
| 135 | + fast = fast.next.next |
| 136 | +
|
| 137 | + # Split the list into two halves |
| 138 | + prev.next = None |
| 139 | +
|
| 140 | + # Recursively sort the two halves |
| 141 | + l1 = self.sortList(head) |
| 142 | + l2 = self.sortList(slow) |
| 143 | +
|
| 144 | + # Merge the sorted halves |
| 145 | + return self.merge(l1, l2) |
| 146 | +
|
| 147 | + def merge(self, l1: ListNode, l2: ListNode) -> ListNode: |
| 148 | + dummy = ListNode() |
| 149 | + curr = dummy |
| 150 | +
|
| 151 | + while l1 and l2: |
| 152 | + if l1.val <= l2.val: |
| 153 | + curr.next = l1 |
| 154 | + l1 = l1.next |
| 155 | + else: |
| 156 | + curr.next = l2 |
| 157 | + l2 = l2.next |
| 158 | + curr = curr.next |
| 159 | +
|
| 160 | + if l1: |
| 161 | + curr.next = l1 |
| 162 | + if l2: |
| 163 | + curr.next = l2 |
| 164 | +
|
| 165 | + return dummy.next |
| 166 | +``` |
| 167 | + |
| 168 | +## Java Implementation |
| 169 | + |
| 170 | +```java |
| 171 | +class Solution { |
| 172 | + public ListNode sortList(ListNode head) { |
| 173 | + if (head == null || head.next == null) |
| 174 | + return head; |
| 175 | + |
| 176 | + ListNode slow = head, fast = head, temp = null; |
| 177 | + |
| 178 | + while (fast != null && fast.next != null) { |
| 179 | + temp = slow; |
| 180 | + slow = slow.next; |
| 181 | + fast = fast.next.next; |
| 182 | + } |
| 183 | + temp.next = null; |
| 184 | + |
| 185 | + ListNode l1 = sortList(head); |
| 186 | + ListNode l2 = sortList(slow); |
| 187 | + |
| 188 | + return merge(l1, l2); |
| 189 | + } |
| 190 | + |
| 191 | + private ListNode merge(ListNode l1, ListNode l2) { |
| 192 | + ListNode dummy = new ListNode(0); |
| 193 | + ListNode curr = dummy; |
| 194 | + |
| 195 | + while (l1 != null && l2 != null) { |
| 196 | + if (l1.val <= l2.val) { |
| 197 | + curr.next = l1; |
| 198 | + l1 = l1.next; |
| 199 | + } else { |
| 200 | + curr.next = l2; |
| 201 | + l2 = l2.next; |
| 202 | + } |
| 203 | + curr = curr.next; |
| 204 | + } |
| 205 | + if (l1 != null) { |
| 206 | + curr.next = l1; |
| 207 | + } |
| 208 | + if (l2 != null) { |
| 209 | + curr.next = l2; |
| 210 | + } |
| 211 | + return dummy.next; |
| 212 | + } |
| 213 | +} |
| 214 | +``` |
| 215 | + |
| 216 | +## JavaScript Implementation |
| 217 | + |
| 218 | +```javascript |
| 219 | +function ListNode(val, next = null) { |
| 220 | + this.val = val; |
| 221 | + this.next = next; |
| 222 | +} |
| 223 | + |
| 224 | +var sortList = function (head) { |
| 225 | + if (!head || !head.next) return head; |
| 226 | + |
| 227 | + let slow = head, |
| 228 | + fast = head, |
| 229 | + temp = null; |
| 230 | + |
| 231 | + while (fast && fast.next) { |
| 232 | + temp = slow; |
| 233 | + slow = slow.next; |
| 234 | + fast = fast.next.next; |
| 235 | + } |
| 236 | + temp.next = null; |
| 237 | + |
| 238 | + const l1 = sortList(head); |
| 239 | + const l2 = sortList(slow); |
| 240 | + |
| 241 | + return merge(l1, l2); |
| 242 | +}; |
| 243 | + |
| 244 | +function merge(l1, l2) { |
| 245 | + const dummy = new ListNode(0); |
| 246 | + let curr = dummy; |
| 247 | + |
| 248 | + while (l1 && l2) { |
| 249 | + if (l1.val <= l2.val) { |
| 250 | + curr.next = l1; |
| 251 | + l1 = l1.next; |
| 252 | + } else { |
| 253 | + curr.next = l2; |
| 254 | + l2 = l2.next; |
| 255 | + } |
| 256 | + curr = curr.next; |
| 257 | + } |
| 258 | + if (l1) { |
| 259 | + curr.next = l1; |
| 260 | + } |
| 261 | + if (l2) { |
| 262 | + curr.next = l2; |
| 263 | + } |
| 264 | + return dummy.next; |
| 265 | +} |
| 266 | +``` |
0 commit comments