2

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();
   }
Hmm
  • 79
  • 6
  • while your code can be improved a lot it works actually. Now coming to the question please clarify more about it especially this statement : "Through doing head.getValue(), I know it adds value to head, but it still prints that head = null." you need to provide code that calls your add and print methods so others can see where the problem is. – SomeDude Sep 22 '18 at 23:01

2 Answers2

2

Java is pass-by-value. Hence when you create a new object reference for head inside the add method ends at the end of the method,

public static void add(ListNode <Integer> head, Integer value) {
  if (head == null)
  {  
     head = new ListNode <Integer> (value, null);//creates new reference
     head.setNext(null);
  } else {
     while (head.getNext() != null)
     {
        head = head.getNext();
     }
     head.setNext(new ListNode <Integer> (value, null));
  }
}

Possible solution is, Initialize the head during the method call itself,
Your add method

public static void add(ListNode <Integer> head, Integer value) {
    while (head.getNext() != null){
        head = head.getNext();
    }
    head.setNext(new ListNode <Integer> (value, null));
}

And during your call

if (head == null) {  
 head = new ListNode <Integer> (value, null);
 head.setNext(null);
}
else add(head,value);
Praveen
  • 1,791
  • 3
  • 20
  • 33
0

This looks like a pass-by-reference issue. Briefly, instantiating a new object is the one place when pass-by-reference does not work.

When you pass an object reference to a method, the method is getting a new pointer to the object. Operations on the object will affect both "versions" of the object because the method and global are pointing to the same object.

But if you re-initialize that pointer (with the "new" keyword) inside the method, the method's pointer is now pointing to the new object, and the original pointer outside the method is not updated.

To fix your error, you'd need to handle the "head is null" case outside the "add" method.

Without Haste
  • 507
  • 2
  • 6