import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    public ListNode removeElements (ListNode head, int val) {
        // write code here
        if(head==null)return null;
        while(head.val==val&&head!=null){
            head = head.next;
        }
        ListNode res = head;
        while(head!=null&&head.next!=null){
            ListNode temp1 = head.next;
            if(temp1.val==val){
                head.next = head.next.next;
            }
            else{
                head = head.next;
            }
        }
        return res;
    }
}