【问题标题】:mergesort using linkedList not working correctly - implemented in Java使用linkedList的合并排序不能正常工作-用Java实现
【发布时间】:2014-01-31 00:40:00
【问题描述】:

我已经为自定义 LinkedIntList 实现了合并排序方法,它只是一个 LinkedList 类。我很难理解为什么它会失败。也就是说,它不会产生任何结果。我多次浏览了代码,但我无法弄清楚。

代码如下:

class ListNode {
 public int data;       // data stored in this node
 public ListNode next;  // link to next node in the list

 // post: constructs a node with data 0 and null link
 public ListNode() {
     this(0, null);
 }

 // post: constructs a node with given data and null link
 public ListNode(int data) {
     this(data, null);
 }

 // post: constructs a node with given data and given link
 public ListNode(int data, ListNode next) {
     this.data = data;
     this.next = next;
 }
}

LinkedIntList 类:

public class LinkedIntList {
 public ListNode front;

 // Constructs an empty list.
 public LinkedIntList() {
     this(null);
 }

 public LinkedIntList(ListNode node){
     front=node;
 }

 public String toString() {
 if (front == null) {
     return "[]";
 } else {
     String result = "[" + front.data;
     ListNode current = front.next;
     while (current != null) {
         result += ", " + current.data;
         current = current.next;
     }
     result += "]";
     return result;
 }


  public void sort(){
     front=mergeSort(front); 
 }

    public ListNode mergeSort(ListNode node) {
        //ystem.out.println("merge sort is called");
        //first get middle of linkedlist, using two pointers
        //you can also get this by size/2

        //step 1: get middle pointers
        if (node==null || node.next==null)
            return null;
        ListNode runner=node.next.next;
        ListNode walker=node;
        while(runner!=null && runner.next!=null){
            runner=runner.next.next;
            walker=walker.next;
        }

        //At this point walker is in center
        ListNode right=walker.next;
        walker.next=null;
        ListNode left=node;
        left=mergeSort(left);
        right=mergeSort(right);

        node=merge(left,right);

        return node;
    }

    //merge of two linkedlist happens here
    private ListNode merge(ListNode l1, ListNode l2) {

        if (l1==null){
            return l2;
        }
        if (l2==null){
            return l1;
        }

        ListNode head=null;
        ListNode curr=null;
        ListNode temp=null;

        while(l1!=null && l2!=null){
            temp=(l1.data < l2.data ? l1 : l2);
            if (head==null){
                head=temp;
                curr=head;
            }
            else {
                curr.next=temp;
                curr=curr.next;
            }
            if (temp==l1){
                l1=l1.next;
            }
            else l2=l2.next;
        }
        if (l1!=null){
            curr.next=l1;
        }
        if (l2!=null){
            curr.next=l2;
        }

        return head;
    }
}

对此进行测试的实用程序类:

public class UtilityMain {

     public static void main(String[] args){

         ListNode node5 = new ListNode(1,null);
         ListNode node4=new ListNode(2,node5);
         ListNode node3=new ListNode(3,node4);
         ListNode node2=new ListNode(4,node3);
         ListNode node1 = new ListNode(5,node2);

         LinkedIntList l = new LinkedIntList(node1);
         System.out.println("before sorting " + l);
             l.sort();
         System.out.println("Afer sorting " + l);

     }
}

输出:--->

before sorting [5, 4, 3, 2, 1]
             After sorting []

【问题讨论】:

  • 哦,我在调试器上苦苦挣扎,现在我唯一的难民就是这样......这就是我在这里发帖的原因!
  • 不要对链表进行排序!
  • @alfasin:我不能告诉面试官。对不起!
  • 引用 Steve Yegge 的话:“看在上帝的份上,不要在面试时尝试对链表进行排序。” steve-yegge.blogspot.com/2008/03/get-that-job-at-google.html

标签: java algorithm sorting linked-list mergesort


【解决方案1】:

至少部分问题出在这里:

if (node==null || node.next==null)
    return null;

如果当前节点为null,则应返回null, 但是,如果 node.next 为 null,则应返回 node。

考虑列表 [3, 2] 的情况

列表节点为(NODE [3], Next -> NODE [2], Next -> [NULL])

你用 Walker = (NODE [3], Next -> [NULL]) 和 Runner = (NODE [2], Next -> [NULL]) 打破列表

然后在 Walker(重命名为 Left)和 Runner(重命名为 Right)上调用归并排序

在每个调用中,Node.next 测试为 null,因此它返回 null 而不是您想要的列表,这应该是传入的内容。

有一个bug会在merge方法中产生一个NULL指针异常..你能找到吗?

【讨论】:

  • 哪个bug会在merge方法中产生NullPointerException?顺便说一句,以上几点解决了问题
  • 好的,我的视觉桌面检查认为还有另一个错误。也许不是。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-11
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多