Skip to content

Latest commit

 

History

History

0203-remove-linked-list-elements

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Problem Description

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

Example:

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

Solution

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

Definition for singly-linked list

public class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

Test

Compile with javac Solution.java and run with java Solution.

This is only for discussion and communication. Please don't use this for submission of assignments.