I know the problem with my code is (should be) stupid. But would appreciate all help.

public void transferFrom(LinkedIntList list2) {
// Point to first node of list1
ListNode current = front;
// Move to the last node of list1
while(current != null) {
current = current.next;
}
// Last node of list1 -> firs node of list2
current.next = list2;
list2 = null;
}
Problem line is current.next = list2;. Data type mismatch because current.next is ListNode and list2 is LinkedIntList.
If I rather use current.next = list2;, I get NullPointerException for this line.
What should I be doing?
EDIT: Fixed!
public void transferFrom(LinkedIntList list2) {
// Point to first node of list1
ListNode current = front;
// Move to the last node of list1
while(current != null && current.next != null) {
current = current.next;
}
// Last node of list1 -> first node of list2
if(front == null) {
front = list2.front;
} else {
current.next = list2.front;
}
list2.front = null;
}
It would help if you posted the class definitions for
LinkedIntListandListNode, and also told us what this method is actually supposed to be doing. But I’m assuming that aLilnkedIntListcontains aListNode frontmember and that you are trying to append the contents oflist2tothis(which is anotherLinkedIntList). Your troublesome line should probably be:However, you have another problem: your
whileloop is guaranteed to exit withcurrent == null, which is not what you want at all. The loop condition should be(current.next != null), not(current != null).Finally, if you want to empty out
list2, the way to do it islist2.front = null;. Assigning tolist2inside the method does nothing.Here’s a version of your code incorporating all of my suggestions: