Java 如何在链表中最后插入元素

Java 如何在链表中最后插入元素,java,linked-list,Java,Linked List,我必须在链表中实现insertaltlast() 我为insertaltlast()编写的代码中出现空指针异常。这可能是因为我访问错误 如何正确访问它并进行插入 class myLinkedList { private static class Node { private int data; private Node next; public Node(int data, Node next) { this.data = data;

我必须在链表中实现
insertaltlast()

我为
insertaltlast()
编写的代码中出现空指针异常。这可能是因为我访问错误

如何正确访问它并进行插入

class myLinkedList {
private static class Node 
{
    private int data; 
    private Node next;

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

private Node head;

// Constructs an empty list
public myLinkedList() 
{
    head = null; 
}

// Returns true if the list is empty otherwise returns false
public boolean isEmpty() 
{
    return (head == null); 
}

// Inserts a new node at the beginning of this list.    
public void insertAtBeginning(int element) 
{
    head = new Node(element, head); 
}

// Returns the first element in the list.
public int getFirstElement() 
{
    if(head == null) 
    {
        System.out.println("Empty linked list"); 
        throw new IndexOutOfBoundsException();
    }
    return head.data; 
}

// Removes the first node in the list.
public int removeFirstNode() 
{ 
    int tmp = getFirstElement(); 
    head = head.next;
    return tmp;
}

// Empties linked list 
public void clear() 
{
    head = null; 
}

// Returns the length of the linked list
public static int LLlength(Node head)
{ 
    int length = 0;
    Node currentNode = head; 

    while(currentNode != null)
    {
        length++;
        currentNode = currentNode.next; 
    }
    return length; 
}

// Displays the linked list elements
public static void display(Node head)
{ 
    if(head == null) 
    {
        System.out.println("Empty linked list");
        throw new IndexOutOfBoundsException(); 
    }

    Node currentNode = head; 

    while(currentNode != null)
    {
        System.out.print(currentNode.data+" ");
        currentNode = currentNode.next; 
    }
    System.out.println();
}

// Displays the linked list elements in reverse order 
public static void displayReverse(Node head)
{
    if(head == null){} 
    else
    {
        Node currentNode = head; 
        displayReverse(currentNode.next); 
        System.out.print(currentNode.data+" ");
    } 
}
//Displays the linked list's last element
public static int getLastElement(Node head)
{
    Node currentNode = head; 

    while(currentNode.next != null)
    {
        currentNode = currentNode.next; 
    }
    return currentNode.data;
}
public static void insertAtLast(Node head,int element)
{
    Node newNode=null;
    newNode.data = element;
    newNode.next = null;
    while(head.next != null)
    {
        head = head.next; 
    }
    head = newNode;
    //return head;

}

//Tells if a sepeific element is in the Linked List or not
public static boolean searchFor(Node head, int element)
{
    Node currentNode = head; 
    boolean flag = false;
    while(currentNode != null)
    {
        if (currentNode.data == element)
        {
            flag = true;
            break;
        } 
        currentNode = currentNode.next;
    }
    return flag;
}

} 

查看如何声明节点对象
newNode
。您将其设置为null,而不是将其实例化为实际对象。请记住,
null
表示引用为空。因此,接下来的两行代码将给您一个错误,因为您试图访问“nothing”的成员变量。因为您有一个
节点
类和构造函数,所以使用它而不是您现在正在做的事情。最后,使用指向head的temp变量,并使用该temp指针遍历节点。因为在之前的例子中,你会将头部设置为指向链表中的新对象,它是最后一个元素,而不是第一个元素

public static void insertAtLast(Node head,int element)
{
    Node newNode= new Node(element,null);
    Node temp = head;
    while(temp.next != null)
    {
        temp = temp.next; 
    }
    temp.next = newNode;
    //return head;

}
您的插入列表方法中存在三个问题


  • 节点newNode=null newNode对象是空引用,它不引用Node的对象, 您可以将其更改为节点newNode=新节点(值,null)
  • 在您的while循环之后,head将与最后一个节点对象进行引用,您的作品将仅使head与新节点对象进行引用,它不会影响列表本身,因为此新对象与列表无关
  • 使用此方法后,头部将引用节点的新对象,而不是列表中的第一个对象

  • 另一种解决方案:

    public static void insertAtLast(Node head,int element)
    {
       Node newNode=new Node(0,null);
       newNode.data = element;
       newNode.next = null;
       Node temp = head;
       while(temp.next != null)
       {
           temp = temp.next; 
       }
       temp.next = newNode;
      //return head;
    }
    

    首先,为什么您的一些方法是静态的?它们作为类方法而不是静态方法是有意义的

    如果您想支持添加到链接列表的末尾,我建议您跟踪列表的
    尾部。现在,您的
    插入列表
    O(n)
    ,您可以通过存储指向链接列表末尾的指针轻松地将其更改为
    O(1)

    但这将增加许多其他方法的复杂性。因此,如果您决定使用
    tail
    指针,则可能必须更新其他指针。我不打算详细介绍您的其他方法,只提供如果您有一个
    尾部
    指针,您的非静态
    插入列表
    会是什么样子

    public void insertAtLast(int element)
    {
        Node newNode = new Node(element,null);
    
        if (head == null){ // you need to check for head, it may be null
           head = newNode;
        }
    
        if (tail == null){ // also, maybe there is no tail currently.
           tail = newNode;
           return;
        }
    
        //there is an existing tail, update it
        tail.next = newNode;
        tail = newNode;
    }