*LeetCode-Remove Linked List Elements

怎么连这个也做不对了

前一个指针 后一个指针 后面那个用来记录previous,记得最开始设置为dummy head

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


你可能感兴趣的:(*LeetCode-Remove Linked List Elements)