Remove Linked List Elements

07/16/2016 Linked List

Question

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

Example:

Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6

Return: 1 –> 2 –> 3 –> 4 –> 5


Solution

Result: Accepted Time: 8 ms

Here should be some explanations.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeElements(struct ListNode* head, int val) {
    struct ListNode* last;
    struct ListNode* now;
    struct ListNode first;
    last = &first;
    now = head;
    first.val = val + 1;
    first.next = head;
    while(now)
    {
        if(now -> val == val)
        {
            last -> next = now -> next;
            free(now);
        }
        else
        {
            last = now;
        }
        now = last -> next;
    }
    return first . next;
}

Complexity Analytics

  • Time Complexity:
  • Space Complexity: