C# Solution Beats 98%


  • 0
    K
     * Definition for singly-linked list.
     * public class ListNode {
     *     public int val;
     *     public ListNode next;
     *     public ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode RemoveElements(ListNode head, int val) 
        {
            ListNode fakeHead = new ListNode(val - 1);
            fakeHead.next = head;
            ListNode currNode = fakeHead;
            
            while(currNode.next != null){
                if (currNode.next.val == val){
                    currNode.next = currNode.next.next;
                }
                else{
                    currNode = currNode.next;
                }
            }
            
            return fakeHead.next;
        }
    }

Log in to reply
 

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