Java clean and easy solution, 1ms


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

  • 0
    K
    This post is deleted!

  • 0
    N
    This post is deleted!

Log in to reply
 

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