删除指定链表节点

来源:互联网 发布:linux内核编译步骤 编辑:程序博客网 时间:2024/05/29 15:20

样例

Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5
class Solution {public:    ListNode *removeElements(ListNode *head, int val) {        // Write your code here        if(head==NULL) return NULL;        ListNode* dummy=new ListNode(-1);        dummy->next=head;        ListNode* cur=head;        ListNode* pre=dummy;        while(cur){            if(cur->val==val){                pre->next=cur->next;                cur=pre->next;            }else{                pre=pre->next;                cur=pre->next;            }        }        return dummy->next;    }};



0 0