1. var sortList = function (head) {
    2. if (!head || !head.next) return head;
    3. let slow = head, fast = head;
    4. let preSlow = null;
    5. while (fast && fast.next) {
    6. preSlow = slow;
    7. slow = slow.next;
    8. fast = fast.next.next;
    9. }
    10. preSlow.next = null;
    11. const l = sortList(head);
    12. const r = sortList(slow);
    13. return merge(l, r);
    14. };
    15. function merge(l1, l2) {
    16. const dummy = new ListNode(0);
    17. let prev = dummy;
    18. while (l1 && l2) {
    19. if (l1.val < l2.val) {
    20. prev.next = l1;
    21. l1 = l1.next;
    22. } else {
    23. prev.next = l2;
    24. l2 = l2.next;
    25. }
    26. prev = prev.next;
    27. }
    28. if (l1) prev.next = l1;
    29. if (l2) prev.next = l2;
    30. return dummy.next;
    31. }