Bug description
I submitted the following C++ code on both NeetCode and LeetCode:
Code
class Solution {
public:
ListNode* reverseList(ListNode* head)
{
ListNode* prev = nullptr;
ListNode* current = head;
ListNode* next = current->next;
while(current != nullptr)
{
current->next = prev;
prev = current;
current = next;
next = current->next;
}
return prev;
}
};
- NeetCode: Accepted — 33/33 test cases
- LeetCode: Runtime Error — UndefinedBehaviorSanitizer
The issue
The issue is caused by accessing current -> next
Since current is nullptr, this is a null pointer dereference and causes undefined behavior.
ListNode* current = head;
ListNode* next = current->next;
Bug description
I submitted the following C++ code on both NeetCode and LeetCode:
Code
The issue
The issue is caused by accessing
current -> nextSince
currentisnullptr, this is a null pointer dereference and causes undefined behavior.