Java - add a node to the end of a list?

前端 未结 8 651
有刺的猬
有刺的猬 2021-01-16 03:55

Here\'s what I have:

public class Node{
    Object data;
    Node next;

    Node(Object data, Node next){
        this.data = data;
        this.next = next         


        
8条回答
  •  离开以前
    2021-01-16 04:20

    In your method to add a node, write a while loop that starts at the head and looks to see if the "next node" is null. If it is not, advance to the "next node" and repeat.

    Once you are at the node that points to nothing, adding the node is as simple as reassigning the null reference to the node to be added.

提交回复
热议问题