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

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

    Node n = head;
    while(n.getNext() != null){
        n = n.getNext();
    }
    n.setNext(nodeToAdd);
    

    That's not sorted, which I can't tell from your question if you want it sorted. Which opens up another whole can of worms such as what do you want to sort on, if you have a linked list of type Object there isn't really anything meaningful to sort on.

提交回复
热议问题