Skip to content

Latest commit

 

History

History
51 lines (41 loc) · 1.18 KB

203_removeLinkedListElement.md

File metadata and controls

51 lines (41 loc) · 1.18 KB

Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.

O(N) Time and O(1) Space

  • Implementation

Code

class Solution{
public:
    ListNode *removeElements(ListNode *head, int val){
        ListNode *curr = head;
        ListNode *prev = NULL;

        while (curr != NULL){
            if (curr->val == val){
                if (prev == NULL)
                    head = curr->next;
                else
                    prev->next = curr->next;
            }
            else{
                prev = curr;
            }
            curr = curr->next;
        }
        return head;
    }
};

O(N) Time and O(1) Space, recursive

  • if head is null return null.
  • if value of current node is val we not include it else we include it.

Code

class Solution{
public:
    ListNode *removeElements(ListNode *head, int val){
        if (head == NULL) return head;
        head->next = removeElements(head->next, val);
        return head->val == val ? head->next : head;
    }
};