Remove Linked List Elements

基础题

注意要bug free看清楚细节得

public class Solution {

    public ListNode removeElements(ListNode head, int val) {

        if(head==null ) return null;

        ListNode h = new ListNode(-1);

        h.next = head;

        ListNode n = head, pre = h;

        while(n!=null){

            if(n.val==val){

                pre.next = n.next;

            }else{

            pre = pre.next;  

            }

            n =pre.next;

        }

        return h.next;

    }

}

 

你可能感兴趣的:(element)