C# - basic iterative


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

Log in to reply
 

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