203. Remove Linked List Elements

 

 

203. Remove Linked List Elements

code

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* cur = head, *pre;//
        while(cur)
        {
            if(cur->val == val)
            {
                if(cur==head) { head = head->next; delete cur; cur = head; }//
                else {pre->next = cur->next; delete cur; cur = pre->next;}//
            }
            else 
            {
                pre = cur;//
                cur = cur->next;
            }
        }
        return head;
    }
};
View Code

 

 

 

1. Leetcode_remove-linked-list-elements;

posted on 2018-12-09 19:59  鹅要长大  阅读(138)  评论(0编辑  收藏  举报

导航