
思路:用头结点解决问题
struct ListNode* removeElements(struct ListNode* head, int val){
struct ListNode header = {0, head};
struct ListNode* ptrHeader = &header;
struct ListNode* ptr = ptrHeader;
struct ListNode* temp;
while(ptr) {
if(ptr->next && ptr->next->val == val){
temp = ptr->next;
ptr->next = ptr->next->next;
free(temp);
}
else
ptr = ptr->next;
}
return ptrHeader->next;
}

思路2:在原链表上进行操作
struct ListNode* removeElements(struct ListNode* head, int val){
struct ListNode* temp = NULL;
while(head && head->val == val) {
head = head->next;
}
struct ListNode* cur = head;
while(cur && cur->next) {
if(cur->next->val == val) {
cur->next = cur->next->next;
} else {
cur = cur->next;
}
}
return head;
}
