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.

&nbsp;
Example 1:

Input: head = [1,2,6,3,4,5,6], val = 6
Output: [1,2,3,4,5]


Example 2:

Input: head = [], val = 1
Output: []


Example 3:

Input: head = [7,7,7,7], val = 7
Output: []


&nbsp;
Constraints:


	The number of nodes in the list is in the range [0, 104].
	1 &lt;= Node.val &lt;= 50
	0 &lt;= val &lt;= 50


# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        prev=head
        cur=head
        while cur is not None:
            if cur.val==val:
                if cur==head:
                    head=head.next
                    prev=head
                else:
                    prev.next=cur.next
            else:
                prev=cur
            cur=cur.next
        return head
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return head;
        }
        ListNode result = head;
        while (head.next != null) {
            if (head.next.val == val) {
                head.next = head.next.next;
            } else {
                head = head.next;
            }
        }
        if (result.val == val) {
            result = result.next;
        }
        return result;
    }
}
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode *prv,*cur,*temp;
        while(head && head->val==val){
            cur=head;
            head=head->next;
            delete(cur);
        }
       if(head==NULL) return head;
        prv=head;
        cur=head->next;
        while(cur){
            if(cur->val==val){
                temp=cur;
                prv->next=cur->next;
                cur=cur->next;
                delete(temp);
            }
            else{
                prv=cur;
                cur=cur->next;
            }
        }
        return head;

    }
};
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
var removeElements = function(head, val) {
    
    if(!head)return null
    
    //if the val is on the beginning  delete it 
    while( head && head.val===val)head=head.next

    
    let current=head;
    let next=head.next;
    //travers the liste and delete any node has this val    
   while(next){        
        if(next.val===val){
        current.next=next.next
        }
        else current=next
            
        next=next.next
        
    }
    
    return head
};
Remove Linked List Elements