i am solving a leetcode problem, where two sorted linked lists are given and we have to merge them and return a sorted list. I have not yet learned merge sort, and tried this question with brute force approach. I want to know why am i getting runtime error: applying non-zero offset to null pointer.
Код: Выделить всё
ListNode* temp = list1;
vector a;
while(temp != NULL)
{
a.push_back(temp->val);
temp = temp->next;
}
temp = list2;
while(temp != NULL)
{
a.push_back(temp->val);
temp = temp->next;
}
sort(a.begin(),a.end(), greater());
ListNode* head = new ListNode(a.back());
a.pop_back();
temp = head;
while(!a.empty())
{
ListNode* n = new ListNode(a.back());
temp->next = n;
temp = temp->next;
a.pop_back();
}
return head;
Источник: https://stackoverflow.com/questions/781 ... t-question