📣 独立日限时特惠: VIP通行证立减$68
查看: 3400| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] Merge sort list, runtime error,哪里错了呢

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 dreamhit 于 2014-3-30 15:47 编辑

在本地电脑上运行可以输出正确结果,但是submit后 总是提示 Runtime Error Last executed input:{}。
请问这错误是什么意思,如何改呢? 谢谢了 ~

题目:Sort a linked list in O(n log n) time using constant space complexity.public class Solution {
    public ListNode sortList(ListNode head) {
        if(head == null)
            return null;
        int len = 0;
        ListNode curr = head;
        while(curr!=null){
            curr = curr.next;
            len++;
        }
        return sortList(head, len);
    } // end method

    ListNode sortList(ListNode head, int len){
        if(len==1){
            head.next = null;
            return head;
        }
        if(len > 1){
            int mid = len/2;
            ListNode part1 = head;
            ListNode part2 = head;
            while(mid!=0){
                mid--;
                part2 = part2.next;
            }
            mid =len/2;
            ListNode list1 = sortList(part1, mid);
            ListNode list2 = sortList(part2, len-mid);
            ListNode newlist = merge(list1, list2);
            return newlist;
        }
        return null;
    } // end method

    // merge two sorted list
    ListNode merge(ListNode list1, ListNode list2) {
                if (list1 == null)
                        return list2;
                if (list2 == null)
                        return list1;

                ListNode head=null;
                if (list1.data < list2.data) {
                        head = list1;
                } else {
                        head = list2;
                        list2 = list1;
                        list1 = head;
                }
                while (list1.next != null && list2 != null) {
                        if (list1.next.data <= list2.data) {
                                list1 = list1.next;
                        } else {
                                ListNode tmp = list1.next;
                                list1.next = list2;
                                list2 = tmp;
                        }
                }
                if (list1.next == null)
                        list1.next = list2;
                return head;
        } // end merge
} // end class.
class ListNode{
    int data;
    ListNode next;
    public ListNode(int d){
        data = d;
    }
}






复制代码


上一篇:对于递归的迷惑
下一篇:想了一下午写出三柱汉诺塔动态规划解法什么水平。。。
🔗
uuuouou 2014-3-30 22:54:21 | 只看该作者
全局:
ListNode sortList(ListNode head, int len){
        if(len==1){
            head.next = null;
            return head;
        }
        if(len > 1){
            int mid = len/2;
            ListNode part1 = head;
            ListNode part2 = head;
            while(mid!=0){
                mid--;
                part2 = part2.next;
            }
            mid =len/2;
            ListNode list1 = sortList(part1, mid);     //分治之前,要先把链表断开吧?
            ListNode list2 = sortList(part2, len-mid);
            ListNode newlist = merge(list1, list2);
            return newlist;
        }
        return null;
    } // end method
回复

使用道具 举报

🔗
 楼主| dreamhit 2014-3-31 07:44:37 | 只看该作者
全局:
uuuouou 发表于 2014-3-30 22:54
ListNode sortList(ListNode head, int len){
        if(len==1){
            head.next = null;

谢谢回复。
虽然没断开,但是实现了排序的功能啊。 我在本地机上测试,输出的sorted list是对的啊。不知道为什么submit 到leetcode后 总是提示runtime error。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表