LeetCode 148. Sort List(链表排序)

原题网址:https://leetcode.com/problems/sort-list/

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

方法一:分区排序,以第一个元素为参照,分为大中小。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public Link sort(ListNode head) {
        if (head == null || head.next == null) {
            Link link = new Link(head, head);
            return link;
        }
        int ref = head.val;
        ListNode less = null, lessCurrent = null;
        ListNode same = null, sameCurrent = null;
        ListNode larger = null, largerCurrent = null;
        ListNode current = head;
        while (current != null) {
            ListNode next = current.next;
            if (current.val < ref) {
                if (less == null) {
                    less = current;
                    lessCurrent = current;
                    lessCurrent.next = null;
                } else {
                    lessCurrent.next = current;
                    lessCurrent = current;
                    lessCurrent.next = null;
                }
            } else if (current.val > ref) {
                if (larger == null) {
                    larger = current;
                    largerCurrent = current;
                    largerCurrent.next = null;
                } else {
                    largerCurrent.next = current;
                    largerCurrent = current;
                    largerCurrent.next = null;
                }
            } else {
                if (same == null) {
                    same = current;
                    sameCurrent = current;
                    sameCurrent.next = null;
                } else {
                    sameCurrent.next = current;
                    sameCurrent = current;
                    sameCurrent.next = null;
                }
            }
            current = next;
        }
        if (less == null && larger == null) {
            Link link = new Link(same, sameCurrent);
            return link;
        } else if (less == null) {
            Link largerLink = sort(larger);
            sameCurrent.next = largerLink.head;
            Link link = new Link(same, largerLink.tail);
            return link;
        } else if (larger == null) {
            Link lessLink = sort(less);
            lessLink.tail.next = same;
            Link link = new Link(lessLink.head, sameCurrent);
            return link;
        } else {
            Link lessLink = sort(less);
            Link largerLink = sort(larger);
            lessLink.tail.next = same;
            sameCurrent.next = largerLink.head;
            Link link = new Link(lessLink.head, largerLink.tail);
            return link;
        }
    }
    public ListNode sortList(ListNode head) {
        if (head == null || head.next == null) return head;
        return sort(head).head;
    }
}
class Link {
    ListNode head, tail;
    Link(ListNode head, ListNode tail) {
        this.head = head;
        this.tail = tail;
    }
}

方法二:快速排序,交换节点的值。

代码略。


方法三:快速排序,改变节点(交换节点)。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    private void sort(ListNode from, ListNode to) {
        if (from == null || from == to || from.next == to || from.next.next == to) return;
        int pivot = from.next.val;
        ListNode small = from;
        ListNode equal = from.next;
        ListNode node = equal;
        while (node.next != to) {
            if (node.next.val < pivot) {
                ListNode nodeNext = node.next.next;
                ListNode smallNext = small.next;
                small.next = node.next;
                node.next.next = smallNext;
                node.next = nodeNext;
                small = small.next;
            } else if (node.next.val == pivot) {
                if (equal == node) {
                    equal = node.next;
                    node = node.next;
                } else {
                    ListNode nodeNext = node.next.next;
                    ListNode equalNext = equal.next;
                    equal.next = node.next;
                    node.next.next = equalNext;
                    node.next = nodeNext;
                    equal = equal.next;
                }
            } else {
                node = node.next;
            }
        }
        sort(from, small.next);
        sort(equal, to);
    }
    public ListNode sortList(ListNode head) {
        ListNode start = new ListNode(0);
        start.next = head;
        sort(start, null);
        return start.next;
    }
}


你可能感兴趣的:(链表,链表排序,排序,分区排序,快速排序法)