I am trying to delete all those elements with a specific value. Here's my code. This code is not working. Can someone help me? TIA. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head == null){
return null;
}
ListNode curr = head;
ListNode prev = null;
while(curr != null){
if(curr.val == val){
prev.next = curr.next;
}
else{
prev = curr;
}
curr = curr.next;
}
return head;
}
}