Remove Linked List Elements

Description

Remove all elements from a linked list of integers that have value val.

Example:

Input: 1->2->6->3->4->5->6, val = 6

Output: 1->2->3->4->5

Code

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        
        ListNode myHead = new ListNode(0);
        myHead.next = head;
        
        ListNode pre = myHead;
        
        while (pre.next != null) {
            if (pre.next.val == val) pre.next = pre.next.next;
            else pre = pre.next;
        }
        
        return myHead.next;
    }
}

Last updated

Was this helpful?