s4553711
4/4/2017 - 2:36 PM

203.cpp

/**
 * 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) {
        if (head == NULL) return head;
        ListNode* h = new ListNode(0);
        h->next = head;
        ListNode* prev = h;
        while(head != NULL) {
            if (head->val == val) {
                prev->next = head->next;
                //delete c;
            } else {
                prev = head;
            }
            head = head->next;
        }
        return h->next;
    }
};