Вы добавляете в узел 20 ll.AddBeginning(20);
в LinkedList.head
и пытаетесь напечатать список, используя Tester.head (declared in main method)
Решение:
Шаг 1: Инициализируйте LinkedList.head либо
- Using constructor or
LinkedList ll = new LinkedList(head);
public LinkedList(Node head) {
this.head = head;
}
- Assign head to LinkedList object i.e., ll
ll.head = head; // Need to initialize Linked List with Head
Шаг 2: Нет необходимости передавать переменную head в функцию printList (). Он печатает элементы LinkedList.head
ll.printList(); //Here you passing Tester.head not required
Код:
Основной метод тестирования
ll.head = head; // Need to initialize Linked List with Head
ll.AddBeginning(20); //Here actually adding node to linkedList head
ll.printList(); //Here you passing Tester.head not required
Метод LinkedList.PrintList
public void printList() {
Node current = this.head;//changed to linkedlist.head
while (current != null) {
System.out.print(current.data + "-->");
current = current.next;
}
System.out.print(current);
}