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

前端 未结 8 757
有刺的猬
有刺的猬 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:39

    A recursive solution:

    public void addToEnd(Object data){
        if (next==null) 
          next = new Node(data, null);
        else
          next.addToEnd(data);
    }
    

提交回复
热议问题