My simple Java Solution


  • 0
    J
    public class Solution {
            public ListNode removeElements(ListNode head, int val) {
                if (head == null) return head;
                
                ListNode dummy = new ListNode(0);
                dummy.next = head;
                ListNode prev = dummy;
                
                while (prev.next != null) {
                    if (prev.next.val == val) {
                        prev.next = prev.next.next;
                    } else {
                        prev = prev.next;
                    }
                }
                
                return dummy.next;
            }
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.