美文网首页
LeetCode 203.移除链表元素

LeetCode 203.移除链表元素

作者: 饼干不干 | 来源:发表于2019-06-04 23:31 被阅读0次

删除链表中等于给定值 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;
}

相关文章

网友评论

      本文标题:LeetCode 203.移除链表元素

      本文链接:https://www.haomeiwen.com/subject/ecudxctx.html