singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode *middleNode(ListNode *head)
{
ListNode *slow_ptr = head;
ListNode *fast_ptr = head;
while (fast_ptr->next != nullptr)
{
slow_ptr = slow_ptr->next;
fast_ptr = fast_ptr->next->next;
}
return slow_ptr;
}
};
And I got the following error,
Line 18: Char 26: runtime error: member access within null pointer of type 'ListNode' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:27:26
Is this because I'm not first checking if the pointer points to a valid ListNode ?
Стикер
The constraint you have is that the linked list must have an even length
Обсуждают сегодня