【LeetCode】 203. Remove Linked List Elements C语言

来源:互联网 发布:mac导入铃声 编辑:程序博客网 时间:2024/06/06 07:21


/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* removeElements(struct ListNode* head, int val) {    struct ListNode *dummy=(struct ListNode *)malloc(sizeof(struct ListNode));    dummy->next=head;        struct ListNode *pre=dummy;   // struct ListNode *Next=NULL;    struct ListNode *cur=head;        while(cur)    {       // Next=cur->next;                if(cur->val == val)        {            pre->next=cur->next; //跳过相同节点        }        else        {            pre=cur;         }        cur=cur->next;  //向后移一个    }    return dummy->next;}


0 0
原创粉丝点击