删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
C
struct ListNode* removeElements(struct ListNode* head, int val){
while(head!=NULL&&head->val==val){
head=head->next;
}
if(head==NULL)
return NULL;
struct ListNode *p = head;
struct ListNode *q = p->next;
while(p&&q){
q = p->next;
while(q != NULL && q->val == val) {
q = q->next;
}
p->next = q;
p = q;
}
return head;
}
网友评论