What the problem in my c program?


  • -1
    C

    struct ListNode* removeElements(struct ListNode* head, int val) {
    if(head==NULL) return NULL;
    struct ListNode *tmphead;
    tmphead->next=head;
    struct ListNode *p=tmphead;
    while (p->next!=NULL)
    {
    if(p->next->val==val)
    {
    p->next=p->next->next;
    }
    else{
    p=p->next;
    }
    return tmphead->next;
    }
    }


  • 0
    S

    tmphead is a pointer. its not allocated any memory and ur trying to access temphead->next; (which is error). Also your logic is messy. Take an example and try it out.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.