148. Sort List

解法一:

public class Solution {
    public ListNode sortList(ListNode head) {
        if (head == null || head.next == null)
            return head;

        ListNode fast = head, slow = head;
        while(fast != null && fast.next != null && fast.next.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }

        ListNode temp = slow.next;
        slow.next = null;
        return mergeList(sortList(head), sortList(temp));
    }

    public ListNode mergeList(ListNode list1, ListNode list2){
        ListNode dummyHead = new ListNode(-1);
        ListNode cur = dummyHead;
        while(list1 != null && list2 != null){
            if (list1.val < list2.val){
                cur.next = list1;
                list1 = list1.next;
            }else{
                cur.next = list2;
                list2 = list2.next;
            }
            cur.next.next = null;
            cur = cur.next;
        }
        cur.next = (list1 == null) ? list2 : list1;
        return dummyHead.next;
    }
}

过不了的代码:不知道为什么会超内存,可能是比解法一递归时多生成了一些变量。

public class Solution {
    public ListNode sortList(ListNode head) {
        return sortList(head, null);
    }

    public ListNode sortList(ListNode left, ListNode right){
        ListNode fast, slow, sortedLeft, sortedRight, temp;
        if (left != null && left.next != null){
            fast = left;  slow = left;
            while(fast != null && fast.next != null && fast.next.next != null){
                fast = fast.next.next;
                slow = slow.next;
            }

            temp = slow.next;
            slow.next = null;
            sortedLeft = sortList(left, temp);  // sort the left half
        }else
            sortedLeft = left;

        if (right != null && right.next != null){
            fast = right;  slow = right;
            while(fast != null && fast.next != null && fast.next.next != null){
                fast = fast.next.next;
                slow = slow.next;
            }

            temp = slow.next;
            sortedRight = sortList(right, temp);  // sort the right half
        }else
            sortedRight = right;

        ListNode formHead = new ListNode(-1);
        formHead.next = sortedLeft;
        ListNode cur = formHead;
        while(cur.next != null && sortedRight != null){
            while(cur.next != null && cur.next.val <= sortedRight.val){
                cur = cur.next;
            }

            //sortedRight should be insert behind cur
            temp = sortedRight.next;
            sortedRight.next = cur.next;
            cur.next = sortedRight;
            cur = (cur.next.next == null) ? cur.next : cur.next.next;   //cur.next.next may be null, don't make that happen
            sortedRight = temp;
        }
        if (cur.next == null){
            cur.next = sortedRight;
        }
        return formHead.next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值