【问题标题】:Sorting a linked list of 0s, 1s and 2s in java not working在java中对0、1和2的链表进行排序不起作用
【发布时间】:2018-09-03 00:07:05
【问题描述】:

我对这个编程世界和 Java 完全陌生,我在尝试链接列表时遇到了一个问题,同时尝试对 0、1 和 2 的链接列表进行排序。谁能说明我做错了什么?我在这里附上了我的代码。这件事基于我的逻辑,它应该可以正常工作,但不幸的是它实际上是在返回列表而不进行排序。

对0、1、2的链表排序

class LinkedList
{
    Node head; 
    class Node
    {
        int data;
        Node next;
        Node(int d) {
          data = d; 
         next = null;
       }



    Node sortList(Node h)
        {
    if(h!=null || h.next!=null)
    {
            return h;
    }
    Node zero = new Node(0);
    Node  one = new Node(0);
    Node  two = new Node(0);
    Node  curr = h;

    while(curr!=null)
    {
        if(curr.data == 0)
        {
            zero.next = curr;
            zero = zero.next;
            curr = curr.next;
        }
        else if(curr.data == 1)
        {
            one.next = curr;
            one = one.next;
            curr = curr.next;
        }
        else
        {
            two.next = curr;
            two = two.next;
            curr = curr.next;
        }
    }

    zero.next = (one.next !=null) ? (one.next): (two.next);
    one.next = two.next;
    two.next = null;
    h = zero.next;
    return h;


}               

    /* Inserts a new Node at front of the list. */
    public void push(int new_data)
    {

        Node new_node = new Node(new_data);

        new_node.next = head;


        head = new_node;
    }

    void printList(Node h)
    {
        Node temp = h;
        while (temp != null)
        {
        System.out.print(temp.data+" ");
        temp = temp.next;
        } 
        System.out.println();
    }


    public static void main(String args[])
    {
        LinkedList llist = new LinkedList();


        llist.push(0);
        llist.push(1);
        llist.push(0);
        llist.push(2);
        llist.push(1);
        llist.push(1);
        llist.push(2);
        llist.push(1);
        llist.push(2);

        System.out.println("Linked List before sorting");
        llist.printList(llist.head);

        Node h=llist.sortList(llist.head);

        System.out.println("Linked List after sorting");
        llist.printList(h);
    }
}

【问题讨论】:

  • 首先排序函数立即退出而不进行排序,因为if(h!=null || h.next!=null)

标签: java data-structures singly-linked-list


【解决方案1】:

我不知道您在代码中的确切含义,但我尝试用尽可能少的更改来做到这一点

我介绍了一个合并两个列表的新功能

private Node merge(Node list1, Node list2){
    if (list1.next == null && list2.next == null)
        return new Node(0);
    else if (list1.next == null)
        return list2;
    else if (list2.next == null)
        return list1;
    else {
        Node curr = list1.next;
        while (curr.next != null){
            curr = curr.next;
        }
        curr.next = list2.next;
        return list1;
    }
}

并将排序功能更改为

Node sortList(Node h)
{
    if (h == null || h.next == null)
        return h;

    Node zero = new Node(0);
    Node one = new Node(1);
    Node two = new Node(2);
    Node curr = h;
    Node nextNode;

    while(curr.next != null) {
        nextNode = curr.next;

        if(curr.data == 0)
        {
            curr.next = zero;
            zero = curr;
        }
        else if(curr.data == 1)
        {
            curr.next = one;
            one = curr;
        }
        else
        {
            curr.next = two;
            two = curr;
        }
        curr = nextNode;
    }

    Node tmpList = merge(zero, one);
    h = merge(tmpList,two);
    return h;


}

这个问题有几个解决方案,其中一些更容易,仅举几例

  • 您可以遍历列表并计算 0、1 和 2 的出现次数,并使用这些数据构建一个新列表
  • 你可以使用各种sorting algorithms并实现它们

【讨论】:

    猜你喜欢
    • 2019-03-13
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 2019-03-22
    • 2012-07-19
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多