在 Java 中为单向链表创建新节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38961553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:57:11  来源:igfitidea点击:

Create new Node for Singly Linked List in Java

javalinked-listnodes

提问by harrisonthu

I am still learning Java, and currently working problems from Cracking the Coding Interview, and one of the problems on Chapter-2 (LinkedList) asks to remove duplicates from an unsorted linked List. I found a bunch of answers/solution on GitHub, but I would like to create my own Node, and write my own version.

我仍在学习 Java,目前正在解决来自 Cracking the Coding Interview 的问题,第 2 章(LinkedList)中的问题之一要求从未排序的链表中删除重复项。我在 GitHub 上找到了一堆答案/解决方案,但我想创建自己的 Node,并编写自己的版本。

What I have implemented so far is that I created Node class and write the function/method that can remove the duplicates from unsorted LinkedList, but when I try to test it, I tried to create the LinkedList in the main function, but I still have no idea how to figure it out. Can someone please help/guide me how to create a Singly LinkedList? Basically, I create four nodes (fourth,third,second,head), and connect them all using the Node class.

到目前为止我实现的是我创建了 Node 类并编写了可以从未排序的 LinkedList 中删除重复项的函数/方法,但是当我尝试测试它时,我尝试在主函数中创建 LinkedList,但我仍然有不知道如何弄清楚。有人可以帮助/指导我如何创建单链表吗?基本上,我创建了四个节点(第四、第三、第二、头部),并使用 Node 类将它们全部连接起来。

Thanks in advance,

提前致谢,

public class Node {
    int data;
    Node next;

    public Node(int data, Node next){
        this.data = data;
        this.next = next;
    }

    public String toString(){
        return data + "";
    }
}


public class problem1 {

    public void Remove_duplicates(Node head){
        if(head == null){
            return;
        }

        Node current = head;
        while(current != null){
            Node runner = current;
            while(runner.next != null){
                if(runner.next.data == current.data){
                    runner.next = runner.next.next;
                }
                else {
                    runner = runner.next;
                }
            }
            current = current.next;
        }
    }

    public static void main(String[] args) {

        Node fourth = new Node(5,null);
        Node third = new Node(3,fourth);
        Node second = new Node(4,third);
        Node head = new Node(3,second);
        for(Node a: head){
            // ERROR: saying can only iterate over an array (or) java.lang.Iterable
            System.out.println(a.toString());
            a = a.next;
        }
    }
}

回答by Kevin Wallis

Try another kind of loop e.g. while

尝试另一种循环,例如 while

Node head = new Node(3, second);
Node node = head;
while (node.next != null) {
    System.out.println(node.toString());
    node = node.next;
}

Like it explains it does not know how to iterate over your nodes. Another approach for using the foreachwould be to create an own class which implements the interface Iterableand does contain your LinkedListlogic.

就像它解释的那样,它不知道如何迭代您的节点。使用 的另一种方法foreach是创建一个自己的类,该类实现接口Iterable并包含您的LinkedList逻辑。

For the second approach I would suggest you to read the following: How can I implement the Iterable interface?

对于第二种方法,我建议您阅读以下内容:如何实现 Iterable 接口?