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;
}