题目描述

原题链接

给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。

示例 1:
148. 排序链表 - 图1
输入:head = [4,2,1,3]
输出:[1,2,3,4]

示例 2:
148. 排序链表 - 图2
输入:head = [-1,5,3,4,0]
输出:[-1,0,3,4,5]

示例 3:
输入:head = []
输出:[]

提示:

  • 链表中节点的数目在范围 [0, 5 * 104] 内
  • -105 <= Node.val <= 105

进阶:你可以在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序吗?

个人解法

Javascript

  1. /*
  2. * @lc app=leetcode.cn id=148 lang=javascript
  3. *
  4. * [148] 排序链表
  5. */
  6. // @lc code=start
  7. /**
  8. * Definition for singly-linked list.
  9. * function ListNode(val, next) {
  10. * this.val = (val===undefined ? 0 : val)
  11. * this.next = (next===undefined ? null : next)
  12. * }
  13. */
  14. /**
  15. * @param {ListNode} head
  16. * @return {ListNode}
  17. */
  18. var sortList = function (head) {
  19. if (!head) {
  20. return null;
  21. }
  22. let myArr = [];
  23. let temp = head;
  24. while (temp) {
  25. myArr.push(temp);
  26. temp = temp.next;
  27. }
  28. myArr.sort((a, b) => {
  29. return a.val - b.val;
  30. })
  31. let res = myArr[0];
  32. let nowTemp = res;
  33. for (let i = 1; i < myArr.length; i++) {
  34. nowTemp.next = myArr[i];
  35. nowTemp = myArr[i];
  36. }
  37. nowTemp.next = null;
  38. return res;
  39. };
  40. // @lc code=end

Java

其他解法

Java

Javascript

归并排序

  1. const merge = (head1, head2) => {
  2. const dummyHead = new ListNode(0);
  3. let temp = dummyHead, temp1 = head1, temp2 = head2;
  4. while (temp1 !== null && temp2 !== null) {
  5. if (temp1.val <= temp2.val) {
  6. temp.next = temp1;
  7. temp1 = temp1.next;
  8. } else {
  9. temp.next = temp2;
  10. temp2 = temp2.next;
  11. }
  12. temp = temp.next;
  13. }
  14. if (temp1 !== null) {
  15. temp.next = temp1;
  16. } else if (temp2 !== null) {
  17. temp.next = temp2;
  18. }
  19. return dummyHead.next;
  20. }
  21. const toSortList = (head, tail) => {
  22. if (head === null) {
  23. return head;
  24. }
  25. if (head.next === tail) {
  26. head.next = null;
  27. return head;
  28. }
  29. let slow = head, fast = head;
  30. while (fast !== tail) {
  31. slow = slow.next;
  32. fast = fast.next;
  33. if (fast !== tail) {
  34. fast = fast.next;
  35. }
  36. }
  37. const mid = slow;
  38. return merge(toSortList(head, mid), toSortList(mid, tail));
  39. }
  40. var sortList = function(head) {
  41. return toSortList(head, null);
  42. };


归并排序2

  1. const merge = (head1, head2) => {
  2. const dummyHead = new ListNode(0);
  3. let temp = dummyHead, temp1 = head1, temp2 = head2;
  4. while (temp1 !== null && temp2 !== null) {
  5. if (temp1.val <= temp2.val) {
  6. temp.next = temp1;
  7. temp1 = temp1.next;
  8. } else {
  9. temp.next = temp2;
  10. temp2 = temp2.next;
  11. }
  12. temp = temp.next;
  13. }
  14. if (temp1 !== null) {
  15. temp.next = temp1;
  16. } else if (temp2 !== null) {
  17. temp.next = temp2;
  18. }
  19. return dummyHead.next;
  20. }
  21. var sortList = function(head) {
  22. if (head === null) {
  23. return head;
  24. }
  25. let length = 0;
  26. let node = head;
  27. while (node !== null) {
  28. length++;
  29. node = node.next;
  30. }
  31. const dummyHead = new ListNode(0, head);
  32. for (let subLength = 1; subLength < length; subLength <<= 1) {
  33. let prev = dummyHead, curr = dummyHead.next;
  34. while (curr !== null) {
  35. let head1 = curr;
  36. for (let i = 1; i < subLength && curr.next !== null; i++) {
  37. curr = curr.next;
  38. }
  39. let head2 = curr.next;
  40. curr.next = null;
  41. curr = head2;
  42. for (let i = 1; i < subLength && curr != null && curr.next !== null; i++) {
  43. curr = curr.next;
  44. }
  45. let next = null;
  46. if (curr !== null) {
  47. next = curr.next;
  48. curr.next = null;
  49. }
  50. const merged = merge(head1, head2);
  51. prev.next = merged;
  52. while (prev.next !== null) {
  53. prev = prev.next;
  54. }
  55. curr = next;
  56. }
  57. }
  58. return dummyHead.next;
  59. };