Trying to sort out the linked lists. There is a class Node in which methods are registered, and there is a class List in which with the help of these methods elements of the list are added. I can not understand how to add more than one element correctly and then output them to the console. Sample code was taken from the linked list

private int data; private Node next; public int getData() { return this.data; } public void setData(int data) { this.data = data; } public void setNext(Node next) { this.next = next; } public Node getNext() { return this.next; } public static void main(String[] args) { Node nin = new Node(); nin.setData(2); System.out.print(nin.getData()); } 

    1 answer 1

     Node head = new Node(); head.setData(2); Node next = new Node(); next.setData(3); head.setNext(next); // Выводим элементы списка в консоль. Node current = head; while (current != null) { System.out.println(current.getData()); current = current.getNext(); } 
    • Thank you very much, now everything has become clear. - Maxim Demyanyuk