在leetcode中删除链表中节点的所有出现

时间:2018-12-21 08:07:45

标签: c linked-list

/usr/lib/x86_64-linux-gnu/crt1.o: In function `_start':

(。text + 0x20):对“ main”的未定义引用    collect2:错误:ld返回1退出状态

当我尝试编译代码时,我看到上述错误。代码负责删除与“ val”匹配的所有节点。如果val与头节点匹配,我就跳过了要删除的条件。这是我看到此错误的原因吗?

struct ListNode* removeElements(struct ListNode* head, int val) {   

struct ListNode *curr = head;
struct ListNode *prev = NULL;
struct ListNode *temp = NULL;
while(curr){        

    while(curr && curr->val != val){

        prev = curr;//keep track of previous node since we need to delete current
        curr = curr->next;           

    }
    if(!curr){
        return temp;
    }
    if(!curr->next){
    prev->next = curr->next;
    }else {
        prev->next = NULL;            
    }
    free(curr);
    curr = prev->next; 

}
    return head;      

}

0 个答案:

没有答案