简体   繁体   English

将节点添加到最初为空的LinkedList

[英]Adding Node to Initially Null LinkedList

I'm having issues when I try to add a node to a linked list that is initialized to null. 尝试将节点添加到初始化为null的链表时遇到问题。 In my method I set a test case to check if the node is initially null and if so it creates a new node with the value that was passed in. But for whatever reason it doesn't work unless the node has atleast one element already passed in. Check it out: 在我的方法中,我设置了一个测试用例,以检查该节点最初是否为空,如果是,则使用传入的值创建一个新节点。但是由于任何原因,除非该节点至少有一个已通过的元素,否则它不起作用签入。签出:

    Node addNode(Node node, int val)
    {
        if(node == null)
            {
                Node newNode = new Node(val);
                //node = newNode;
                return newNode;
            }
        node.next = addNode(node.next, val);
        return node;
    }

//Driver Class
    Scanner in = new Scanner(System.in);
    Node myNode = new Node(1);
    int numEntries = in.nextInt();
    for(int i = 0 ; i < numEntries ; i++)
    {
        int inputVal = in.nextInt();
        myNode.addNode(myNode, inputVal);
    }

The above code will not run if myNode is initialized to a null value (Node myNode = null;) 如果将myNode初始化为空值(Node myNode = null;),则以上代码将无法运行

Full Code: 完整代码:

/* package whatever; // don't place package name! */

   import java.util.*;
   import java.lang.*;
   import java.io.*;




/* Name of the class has to be "Main" only if the class is public. */
    class Ideone
    {
     public static class Node
     {
         private int value;
         Node next;
         public Node()
         {
             next = null;
         }
         public Node(int val)
         {
            value = val;
            next = null;
        }
        Node addNode(Node node, int val)
        {
            if(node == null)
                {
                    Node newNode = new Node(val);
                    //node = newNode;
                    return newNode;
                }
            node.next = addNode(node.next, val);
            return node;
        }
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner in = new Scanner(System.in);
        Node myNode = new Node(1);
        Node current = null;
        Node oddFirst = new Node(1);
        int numEntries = in.nextInt();
        for(int i = 0 ; i < numEntries ; i++)
        {
            int inputVal = in.nextInt();
            myNode.addNode(myNode, inputVal);
        }
        current = myNode;
        while(current != null) // Check if values were copied correctly
        {
            if(oddFirst == null) 
            {
                oddFirst = new Node(current.value);
            }
            oddFirst.addNode(oddFirst,current.value);
            //oddFirst = current.next;
            //oddFirst = oddFirst.next;
            current = current.next.next;
        }
        while(oddFirst != null)
        {
            System.out.println("Current Value: " + oddFirst.value);
            oddFirst = oddFirst.next;
        }
    }
}

A simple solution for a linked list: 链表的简单解决方案:

class Node {
    int val;
    Node next;
}

public class LinkedList {
    public Node first;
    public Node last;
    public void addNext(int val) {
        Node node = new Node();
        node.val = val;
        if(last == null) {
            first = last = node;
        }
        else {
            last.next = node;
            last = node;
        }
    }
}

The main issue with the original code is that it doesn't concern itself with the case of the empty list. 原始代码的主要问题在于,它与空列表无关。 You cannot discern the case where the list is comprised of a single 1 value, and the empty list. 您无法确定列表由单个1值和空列表组成的情况。

Because you're not handling the return value of addNode(). 因为您没有处理addNode()的返回值。

You're returning a Node in the following function: 您将在以下函数中返回节点:

Node addNode(Node node, int val)

but you're not handling the return here: 但您不在此处处理退货:

myNode.addNode(myNode, inputVal);

This should help you figure out the solution. 这应该可以帮助您找出解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM