|
58 | 58 |
|
59 | 59 | <!-- 这里可写通用的实现逻辑 -->
|
60 | 60 |
|
| 61 | +先用快慢指针找到链表中点,然后分成左右两个链表,递归排序左右链表。最后合并两个排序的链表即可。 |
| 62 | + |
61 | 63 | <!-- tabs:start -->
|
62 | 64 |
|
63 | 65 | ### **Python3**
|
64 | 66 |
|
65 | 67 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
66 | 68 |
|
67 | 69 | ```python
|
68 |
| - |
| 70 | +# Definition for singly-linked list. |
| 71 | +# class ListNode: |
| 72 | +# def __init__(self, val=0, next=None): |
| 73 | +# self.val = val |
| 74 | +# self.next = next |
| 75 | +class Solution: |
| 76 | + def sortList(self, head: ListNode) -> ListNode: |
| 77 | + if head is None or head.next is None: |
| 78 | + return head |
| 79 | + slow, fast = head, head.next |
| 80 | + while fast and fast.next: |
| 81 | + slow, fast = slow.next, fast.next.next |
| 82 | + t = slow.next |
| 83 | + slow.next = None |
| 84 | + l1, l2 = self.sortList(head), self.sortList(t) |
| 85 | + dummy = ListNode() |
| 86 | + cur = dummy |
| 87 | + while l1 and l2: |
| 88 | + if l1.val <= l2.val: |
| 89 | + cur.next = l1 |
| 90 | + l1 = l1.next |
| 91 | + else: |
| 92 | + cur.next = l2 |
| 93 | + l2 = l2.next |
| 94 | + cur = cur.next |
| 95 | + cur.next = l1 or l2 |
| 96 | + return dummy.next |
69 | 97 | ```
|
70 | 98 |
|
71 | 99 | ### **Java**
|
72 | 100 |
|
73 | 101 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
74 | 102 |
|
75 | 103 | ```java
|
| 104 | +/** |
| 105 | + * Definition for singly-linked list. |
| 106 | + * public class ListNode { |
| 107 | + * int val; |
| 108 | + * ListNode next; |
| 109 | + * ListNode() {} |
| 110 | + * ListNode(int val) { this.val = val; } |
| 111 | + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } |
| 112 | + * } |
| 113 | + */ |
| 114 | +class Solution { |
| 115 | + public ListNode sortList(ListNode head) { |
| 116 | + if (head == null || head.next == null) { |
| 117 | + return head; |
| 118 | + } |
| 119 | + ListNode slow = head, fast = head.next; |
| 120 | + while (fast != null && fast.next != null) { |
| 121 | + slow = slow.next; |
| 122 | + fast = fast.next.next; |
| 123 | + } |
| 124 | + ListNode t = slow.next; |
| 125 | + slow.next = null; |
| 126 | + ListNode l1 = sortList(head); |
| 127 | + ListNode l2 = sortList(t); |
| 128 | + ListNode dummy = new ListNode(); |
| 129 | + ListNode cur = dummy; |
| 130 | + while (l1 != null && l2 != null) { |
| 131 | + if (l1.val <= l2.val) { |
| 132 | + cur.next = l1; |
| 133 | + l1 = l1.next; |
| 134 | + } else { |
| 135 | + cur.next = l2; |
| 136 | + l2 = l2.next; |
| 137 | + } |
| 138 | + cur = cur.next; |
| 139 | + } |
| 140 | + cur.next = l1 == null ? l2 : l1; |
| 141 | + return dummy.next; |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +### **C++** |
| 147 | + |
| 148 | +```cpp |
| 149 | +/** |
| 150 | + * Definition for singly-linked list. |
| 151 | + * struct ListNode { |
| 152 | + * int val; |
| 153 | + * ListNode *next; |
| 154 | + * ListNode() : val(0), next(nullptr) {} |
| 155 | + * ListNode(int x) : val(x), next(nullptr) {} |
| 156 | + * ListNode(int x, ListNode *next) : val(x), next(next) {} |
| 157 | + * }; |
| 158 | + */ |
| 159 | +class Solution { |
| 160 | +public: |
| 161 | + ListNode* sortList(ListNode* head) { |
| 162 | + if (!head || !head->next) return head; |
| 163 | + auto* slow = head; |
| 164 | + auto* fast = head->next; |
| 165 | + while (fast && fast->next) |
| 166 | + { |
| 167 | + slow = slow->next; |
| 168 | + fast = fast->next->next; |
| 169 | + } |
| 170 | + auto* t = slow->next; |
| 171 | + slow->next = nullptr; |
| 172 | + auto* l1 = sortList(head); |
| 173 | + auto* l2 = sortList(t); |
| 174 | + auto* dummy = new ListNode(); |
| 175 | + auto* cur = dummy; |
| 176 | + while (l1 && l2) |
| 177 | + { |
| 178 | + if (l1->val <= l2->val) |
| 179 | + { |
| 180 | + cur->next = l1; |
| 181 | + l1 = l1->next; |
| 182 | + } |
| 183 | + else |
| 184 | + { |
| 185 | + cur->next = l2; |
| 186 | + l2 = l2->next; |
| 187 | + } |
| 188 | + cur = cur->next; |
| 189 | + } |
| 190 | + cur->next = l1 ? l1 : l2; |
| 191 | + return dummy->next; |
| 192 | + } |
| 193 | +}; |
| 194 | +``` |
| 195 | +
|
| 196 | +### **Go** |
| 197 | +
|
| 198 | +```go |
| 199 | +/** |
| 200 | + * Definition for singly-linked list. |
| 201 | + * type ListNode struct { |
| 202 | + * Val int |
| 203 | + * Next *ListNode |
| 204 | + * } |
| 205 | + */ |
| 206 | +func sortList(head *ListNode) *ListNode { |
| 207 | + if head == nil || head.Next == nil { |
| 208 | + return head |
| 209 | + } |
| 210 | + slow, fast := head, head.Next |
| 211 | + for fast != nil && fast.Next != nil { |
| 212 | + slow, fast = slow.Next, fast.Next.Next |
| 213 | + } |
| 214 | + t := slow.Next |
| 215 | + slow.Next = nil |
| 216 | + l1, l2 := sortList(head), sortList(t) |
| 217 | + dummy := &ListNode{} |
| 218 | + cur := dummy |
| 219 | + for l1 != nil && l2 != nil { |
| 220 | + if l1.Val <= l2.Val { |
| 221 | + cur.Next = l1 |
| 222 | + l1 = l1.Next |
| 223 | + } else { |
| 224 | + cur.Next = l2 |
| 225 | + l2 = l2.Next |
| 226 | + } |
| 227 | + cur = cur.Next |
| 228 | + } |
| 229 | + if l1 != nil { |
| 230 | + cur.Next = l1 |
| 231 | + } else { |
| 232 | + cur.Next = l2 |
| 233 | + } |
| 234 | + return dummy.Next |
| 235 | +} |
| 236 | +``` |
| 237 | + |
| 238 | +### **JavaScript** |
| 239 | + |
| 240 | +```js |
| 241 | +/** |
| 242 | + * Definition for singly-linked list. |
| 243 | + * function ListNode(val, next) { |
| 244 | + * this.val = (val===undefined ? 0 : val) |
| 245 | + * this.next = (next===undefined ? null : next) |
| 246 | + * } |
| 247 | + */ |
| 248 | +/** |
| 249 | + * @param {ListNode} head |
| 250 | + * @return {ListNode} |
| 251 | + */ |
| 252 | +var sortList = function(head) { |
| 253 | + if (!head || !head.next) { |
| 254 | + return head; |
| 255 | + } |
| 256 | + let slow = head; |
| 257 | + let fast = head.next; |
| 258 | + while (fast && fast.next) { |
| 259 | + slow = slow.next; |
| 260 | + fast = fast.next.next; |
| 261 | + } |
| 262 | + let t = slow.next; |
| 263 | + slow.next = null; |
| 264 | + let l1 = sortList(head); |
| 265 | + let l2 = sortList(t); |
| 266 | + const dummy = new ListNode(); |
| 267 | + let cur = dummy; |
| 268 | + while (l1 && l2) { |
| 269 | + if (l1.val <= l2.val) { |
| 270 | + cur.next = l1; |
| 271 | + l1 = l1.next; |
| 272 | + } else { |
| 273 | + cur.next = l2; |
| 274 | + l2 = l2.next; |
| 275 | + } |
| 276 | + cur = cur.next; |
| 277 | + } |
| 278 | + cur.next = l1 || l2; |
| 279 | + return dummy.next; |
| 280 | +}; |
| 281 | +``` |
| 282 | + |
| 283 | +### **C#** |
| 284 | + |
| 285 | +```cs |
| 286 | +/** |
| 287 | + * Definition for singly-linked list. |
| 288 | + * public class ListNode { |
| 289 | + * public int val; |
| 290 | + * public ListNode next; |
| 291 | + * public ListNode(int val=0, ListNode next=null) { |
| 292 | + * this.val = val; |
| 293 | + * this.next = next; |
| 294 | + * } |
| 295 | + * } |
| 296 | + */ |
| 297 | +public class Solution { |
| 298 | + public ListNode SortList(ListNode head) { |
| 299 | + if (head == null || head.next == null) |
| 300 | + { |
| 301 | + return head; |
| 302 | + } |
| 303 | + ListNode slow = head, fast = head.next; |
| 304 | + while (fast != null && fast.next != null) |
| 305 | + { |
| 306 | + slow = slow.next; |
| 307 | + fast = fast.next.next; |
| 308 | + } |
| 309 | + ListNode t = slow.next; |
| 310 | + slow.next = null; |
| 311 | + ListNode l1 = SortList(head); |
| 312 | + ListNode l2 = SortList(t); |
| 313 | + ListNode dummy = new ListNode(); |
| 314 | + ListNode cur = dummy; |
| 315 | + while (l1 != null && l2 != null) |
| 316 | + { |
| 317 | + if (l1.val <= l2.val) |
| 318 | + { |
| 319 | + cur.next = l1; |
| 320 | + l1 = l1.next; |
| 321 | + } |
| 322 | + else |
| 323 | + { |
| 324 | + cur.next = l2; |
| 325 | + l2 = l2.next; |
| 326 | + } |
| 327 | + cur = cur.next; |
| 328 | + } |
| 329 | + cur.next = l1 == null ? l2 : l1; |
| 330 | + return dummy.next; |
| 331 | + } |
| 332 | +} |
| 333 | +``` |
76 | 334 |
|
| 335 | +### **TypeScript** |
| 336 | + |
| 337 | +```ts |
| 338 | +/** |
| 339 | + * Definition for singly-linked list. |
| 340 | + * class ListNode { |
| 341 | + * val: number |
| 342 | + * next: ListNode | null |
| 343 | + * constructor(val?: number, next?: ListNode | null) { |
| 344 | + * this.val = (val===undefined ? 0 : val) |
| 345 | + * this.next = (next===undefined ? null : next) |
| 346 | + * } |
| 347 | + * } |
| 348 | + */ |
| 349 | + |
| 350 | +function sortList(head: ListNode | null): ListNode | null { |
| 351 | + if (head == null || head.next == null) return head; |
| 352 | + // 快慢指针定位中点 |
| 353 | + let slow: ListNode = head, fast: ListNode = head.next; |
| 354 | + while (fast != null && fast.next != null) { |
| 355 | + slow = slow.next; |
| 356 | + fast = fast.next.next; |
| 357 | + } |
| 358 | + // 归并排序 |
| 359 | + let mid: ListNode = slow.next; |
| 360 | + slow.next = null; |
| 361 | + let l1: ListNode = sortList(head); |
| 362 | + let l2: ListNode = sortList(mid); |
| 363 | + let dummy: ListNode = new ListNode(); |
| 364 | + let cur: ListNode = dummy; |
| 365 | + while (l1 != null && l2 != null) { |
| 366 | + if (l1.val <= l2.val) { |
| 367 | + cur.next = l1; |
| 368 | + l1 = l1.next; |
| 369 | + } else { |
| 370 | + cur.next = l2; |
| 371 | + l2 = l2.next; |
| 372 | + } |
| 373 | + cur = cur.next; |
| 374 | + } |
| 375 | + cur.next = l1 == null ? l2 : l1; |
| 376 | + return dummy.next; |
| 377 | +}; |
77 | 378 | ```
|
78 | 379 |
|
79 | 380 | ### **...**
|
|
0 commit comments