删除指定链表节点

样例

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;
    }
};



你可能感兴趣的:(删除指定链表节点)