public ListNode sortList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode slow = head;
        ListNode fast = head.next;

        while (fast != null && fast.next != null) { // slow in middle element
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode start2 = slow.next;
        slow.next = null; // break the link

        ListNode start1 = sortList(head); // sort first half
        ListNode start11 = sortList(start2); // sort second half

        return merge(start1, start11); // merge sorted arrays
    }
This is mergeSort algorithm for linked list. It divides list into two halves using two (slow, fast)
pointer and sort each half.
src/problems/medium/SortList.java