criskgl
9/30/2019 - 4:32 PM

Delete all target values in SINGLE linked list

//deletes all target values found in a list

public ListNode removeElements(ListNode head, int val) {
    ListNode originalHead = head;
    while(head != null){
        if(head.val == val && head == originalHead){
            originalHead = head.next;
            head = head.next;
            continue;
        }
        if(head.next == null) break;
        if(head.next.val == val){
            head.next = head.next.next;
            head = originalHead;
        }else{
            head = head.next;
        }
    }
    return originalHead;
}