我正在尝试为 leetcode 中的“Merge K Sorted Linked Lists”编写代码。 问题链接:这里 但是我收到以下运行时错误:
Line 1034: Char 9: runtime error: reference binding to null pointer of type 'ListNode *' (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:9
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
int i = 0;
ListNode* Head = lists[0];
for(i = 1; i<lists.size(); i++){
ListNode* head = lists[i];
ListNode* Current = new ListNode;
Current->val = 0;
Current->next = Head;
cout << Head->val << '\n';
while(head != NULL){
ListNode * Next = Current->next;
if(Next == NULL || Next->val>=head->val){
ListNode * Temp = head->next;
head->next = Next;
Current->next = head;
Current = head;
head = Temp;
}
else{
Current = Next;
}
}
}
return Head;
}
};
这是我为合并 K 个排序的链表而编写的函数。我可能没有写出正确的逻辑。但是为了弄清楚这一点,我需要了解我在哪里以及为什么会收到上述错误。任何人都请帮我弄清楚