Clear accepted code by using a fakeHeader(JAVA)


  • 0
    public class Solution {
        public ListNode removeElements(ListNode head, int val) {
            if(head == null){
                return head;
            }
            ListNode fakeHead = new ListNode(0);
            ListNode p1 = fakeHead;
            fakeHead.next = head;
            while(head != null){
                if(head.val == val){
                    p1.next = head.next;
                }
                else{
                    p1 = p1.next;
                }
                head = p1.next;
            }
            return fakeHead.next;
        }
    }

Log in to reply
 

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