A C# Solution without surprise


  • 0
    L
    public ListNode RemoveElements(ListNode head, int val) {
        ListNode tmp = head;
        while(tmp != null){
            if(tmp.val == val){
                head = head.next;
                tmp = head;
            }
            else if(tmp.next != null && tmp.next.val == val){
                ListNode tmpNext = tmp.next;
                tmp.next = tmpNext.next;
                tmpNext.next = null;
            }else tmp = tmp.next;
        }
        return head;
    }

Log in to reply
 

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