注意事项
- 要删除的结点相邻
- 第一个结点就是要删除的结点
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/struct ListNode* removeElements(struct ListNode* head, int val){if(head==NULL){return NULL;}struct ListNode *prev=head;struct ListNode *cur = head->next;while(cur!=NULL){ if(cur->val==val){prev->next=cur->next;free(cur);cur=prev->next;}else{prev=cur;cur=cur->next;}}if(head->val == val){struct ListNode *newHead =head->next;free(head);return newHead;}else{return head;}return head;
}