我知道我的代码的问题是(应该)愚蠢的。但会感谢所有帮助。
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;
}
问题线是current.next = list2;
。数据类型不匹配,因为current.next
isListNode
和list2
is LinkedIntList
。
如果我宁愿使用current.next = list2;
,我会得到NullPointerException
这条线。
我应该做什么?
编辑:固定!
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;
}