So I have a method to add a ListNode to an existing ListNode, and it works when it adds to the end when head != null, but as soon as head = null, it prints as if head is null. Through doing head.getValue(), I know it adds value to head, but it still prints that head = null.
public static void add(ListNode <Integer> head, Integer value)
{
if (head == null)
{
head = new ListNode <Integer> (value, null);
head.setNext(null);
} else {
while (head.getNext() != null)
{
head = head.getNext();
}
head.setNext(new ListNode <Integer> (value, null));
}
}
public static void printLinkedList(ListNode <Integer> head)
{
if (head == null)
{
System.out.println();
return;
}
for(; head.getValue() != null; head = head.getNext())
{
System.out.print(head.getValue() + " ");
if(head.getNext() == null)
{
break;
}
}
System.out.println();
}