簡單明了献酗,但要注意刪除頭節(jié)點的情況淮逊,最好使用dummy頭
struct ListNode* removeElements(struct ListNode* head, int val) {
if(head == NULL)
return NULL;
struct ListNode *prev, *curr;
struct ListNode *dummyhead = malloc(sizeof(struct ListNode));
dummyhead->next = head;
prev = dummyhead;
curr = dummyhead->next;
while(curr){
if(curr->val == val){
prev->next = curr->next;
curr = prev->next;
}else{
curr = curr->next;
prev = prev->next;
}
}
return dummyhead->next;
}