Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Remove Linked List Elements leetcode

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        
        /*while (head == NULL){
            return head;
        }
        No need for this funtion since we gotta return head in the end of the funtion anyways so this case wud be included
        */
        
        while (head != NULL && head -> val == val){
            head = head -> next;
        }
        ListNode* curr = head;
        while (curr != NULL && curr -> next != NULL){
            if (curr -> next -> val == val){
                curr -> next = curr -> next -> next;
            }
            else {
                curr = curr -> next;
            }
        }
    return head;
    }
};
 
PREVIOUS NEXT
Tagged: #Remove #Linked #List #Elements #leetcode
ADD COMMENT
Topic
Name
7+9 =