lintcode,链表排序

在 O(n log n) 时间复杂度和常数级的空间复杂度下给链表排序。
样例
给出 1->3->2->null,给它排序变成 1->2->3->null.

解题思路:根据要求采用先sort再merge的方法,首先找到中点,然后从中点两端分别sort,将两个结果进行merge。

一刷ac
归并思路实现

/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */ 
public class Solution {
    /**
     * @param head: The head of linked list.
     * @return: You should return the head of the sorted linked list,
                    using constant space complexity.
     */
    public ListNode sortList(ListNode head) {  
        if(head == null || head.next == null) return head;
        ListNode mid = findMid(head);

        ListNode tmp = mid.next;
        mid.next = null;
        ListNode headleft = sortList(head);
        ListNode headright = sortList(tmp);
        return merge(headleft, headright);

    }
    public static ListNode findMid(ListNode head){
        if(head == null || head.next == null) return head;
        ListNode slow = head;
        ListNode fast = head;
        while(fast.next != null && fast.next.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public static ListNode merge(ListNode left, ListNode right){
        if(left == null || right == null) return null;
        ListNode node = new ListNode(0);
        ListNode dummy = node;
        while(left != null && right != null){
            if(left.val < right.val){
                ListNode tmp = left;
                left = left.next;
                dummy.next = tmp;
                dummy = dummy.next;
            }else{
                ListNode tmp = right;
                right = right.next;
                dummy.next = tmp;
                dummy = dummy.next;
            }
        }
        if(left != null) dummy.next = left;
        else dummy.next = right;
        return node.next;
    }


}

快排思路实现

/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */ 
public class Solution {
    /**
     * @param head: The head of linked list.
     * @return: You should return the head of the sorted linked list,
                    using constant space complexity.
     */
    public ListNode sortList(ListNode head) {  
        if(head == null || head.next == null) return head;
        ListNode pivot = head;
        head = head.next;
        pivot.next = null;
        ListNode p = pivot;

        ListNode small = new ListNode(0);
        ListNode large = new ListNode(0);
        ListNode s = small;
        ListNode l = large;

        while(head != null){
            if(head.val > pivot.val){
                l.next = head;
                l = l.next;
            }else if(head.val == pivot.val){
                p.next = head;
                p = p.next;
            }else{
                s.next = head;
                s = s.next;
            }
            head = head.next;
        }
        s.next = null;
        l.next = null;
        ListNode ss = sortList(small.next);
        if(ss == null){
            ss = pivot;
        }else{
            ListNode tmp = ss;
            while(tmp.next != null){
                tmp = tmp.next;
            }
            tmp.next = pivot;
        }
        ListNode tail = sortList(large.next);
        p.next = tail;
        return ss;

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值