AC Java solution


  • 66
    L
    public class Solution {
        public ListNode removeElements(ListNode head, int val) {
            ListNode fakeHead = new ListNode(-1);
            fakeHead.next = head;
            ListNode curr = head, prev = fakeHead;
            while (curr != null) {
                if (curr.val == val) {
                    prev.next = curr.next;
                } else {
                    prev = prev.next;
                }
                curr = curr.next;
            }
            return fakeHead.next;
        }
    }

  • 5
    J

    I like the idea of using a fakeHead to avoid edge cases.


  • 0
    F
    This post is deleted!

  • 0
    B

    Can I return head instead of fakeHead.next at the last line?

    I tried "return head" but it gave the wrong answer, why?


  • 0
    L

    I am not very familiar with that. Could you please explain a bit more?


  • 0
    L

    The reason is that head may also need to be removed from the list. Is the test case that gave the wrong answer of this kind?


  • 0
    F
    This post is deleted!

  • 1
    E

    Here is my code with similar strategy. I did not connect fakeHead with head at the beginning though.

     public ListNode removeElements(ListNode head, int val) {
        ListNode fakeHead = new ListNode(-1);
        ListNode prev = fakeHead;
        while(head != null) {
            if(head.val == val) {
                head = head.next;
                prev.next = null; // this is necessary!!!
            }
            else {
                prev.next = head;
                prev = head;
                head = head.next;
            }    
        }
        return fakeHead.next;
    }

  • 15
    M

    we can do it with only having curr.

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

  • 2
    C

    public ListNode removeElements(ListNode head, int val) {

        if(null==head) return null;
        ListNode traverser = head;
        
        while(traverser.next!=null){
            if(traverser.next.val==val){
                traverser.next = traverser.next.next;
            } else {
                traverser = traverser.next;
            }
        }
        
        if(head.val==val) return head.next;
        
        return head;
    }

  • 0
    F

    +1 this one. Mine is same as this.


  • 1

    @lx223 This solution is quite smart.


  • 0
    D

    You actually only need one pointer:

    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            if(!head) return nullptr;
            ListNode *dummy = new ListNode(0), *curr = dummy;
            dummy -> next = head;
            while(curr -> next) {
                if(curr -> next -> val == val) curr -> next = curr -> next -> next;
                else curr = curr -> next;
            }
            return dummy -> next;
        }
    };
    

  • 0
    K

    Dummy head idea is awesome!!!


  • 0
    K

    @mscho147 I think ur solution is cleanest.


  • 0
    E

    @borischou

    head may be abandoned...
    such as head.val == val


  • 1
    H

    I like this solution! I think it is a little bit easier to follow than that using recursion.


  • 0
    S

    the runtime beats only 7% for me by using this method. anyone know why it is so slow? I submitted 3 times. still the same.


  • 0

    @lx223 Nice solution, I also came out the same idea in the first time.
    However, be aware of that, you forgot to set the fakeHead null(or delete it).
    So, I'm wondering if this causes possible memory leaking or additionally unused memory over there?


  • 0
    P

    so cool solution


Log in to reply
 

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