148. Sort List (Medium)

https://leetcode.com/problems/sort-list/

Sort a linked list in O(n log n) time using constant space complexity.

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4

Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5

Hints

Solutions

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode sortList(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }

        int n = 0;
        ListNode p = head;
        while(p != null) {
            n++;
            p = p.next;
        }
        int ss = 0;
        p = head;
        ListNode q = head.next;
        while(ss < n / 2 - 1) {
            p = p.next;
            q = q.next;
            ss++;
        }
        p.next = null;
        p = sortList(head);
        q = sortList(q);

        //merge
        ListNode s = new ListNode(-1);
        ListNode t = s;
        while(p != null && q != null) {
            if(p.val <= q.val) {
                t.next = p;
                p = p.next;
                t = t.next;
            } else {
                t.next = q;
                q = q.next;
                t = t.next;
            }
        }
        while(p != null) {
            t.next = p;
            t = t.next;
            p = p.next;
        }
        while(q != null) {
            t.next = q;
            t = t.next;
            q = q.next;
        }

        return s.next;
    }
}
Copyright © iovi.com 2017 all right reserved,powered by GitbookLast Modification: 2019-04-15 19:02:15

results matching ""

    No results matching ""