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

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

    To add to the end, you'd have to walk to the end of the list (i.e., to where next=null) and add a new node there.

    In the real world, you'd use an ArrayList for this and not bother with a linked list or manual structure at all.

提交回复
热议问题