203. Remove Linked List Elements Posted on 2020-05-26 | Edited on 2021-01-22 | Views: https://leetcode.com/problems/remove-linked-list-elements/ 123456789101112131415161718192021222324252627/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummy=new ListNode(-1); ListNode prev=dummy; dummy.next=head; while(head!=null){ if(head.val==val){ prev.next=head.next; head=prev.next; }else{ head=head.next; prev=prev.next; } } return dummy.next; }}