what is it that i am doing wrong as I am getting error.
ide->https://ide.codingblocks.com/s/205570
ques->https://leetcode.com/problems/remove-linked-list-elements/
what is it that i am doing wrong as I am getting error.
ide->https://ide.codingblocks.com/s/205570
ques->https://leetcode.com/problems/remove-linked-list-elements/
mistakes
prev->next = head
u cannot refer to the next of a null element this is a null pointer exception since this does not have an allocated space in the memory
correct code:
if(head==NULL)
{
return head;
}
ListNode* prev =NULL;
//prev->next = head;
ListNode* curr =head;
while(curr != NULL){
if(curr->val == val){
if(curr == head)
{
head = head->next;
curr = head;
continue;
}
else
{
prev->next = curr->next;
curr = curr->next;
}
}
else{
prev = curr;
curr=curr->next;
//prev=prev->next;
}
}
return head;