Lintcode – Remove Linked List Elements

Lintcode – Remove Linked List Elements

Remove all elements from a linked list of integers that have value?val.

c++:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    /**
     * @param head a ListNode
     * @param val an integer
     * @return a ListNode
     */
    ListNode *removeElements(ListNode *head, int val) {
        // Write your code here
        ListNode * dummy = new ListNode(0);
        ListNode * current = dummy;
        current->next = head;
        while (current->next != NULL) {
            if (current->next->val == val) {
                current->next = current->next->next;
            } else {
                current = current->next;
            }
        }
        return dummy->next;
    }
};

 

Leave a Reply

Your email address will not be published.