[leedcode]Remove Linked List Elements

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
  public ListNode removeElements(ListNode head, int val) {
        ListNode p=head;
        if(head==null) return head;
        while(p!=null&&p.val==val) {
            head=head.next;
            p=head;
        }
        while(p!=null&&p.next!=null){
            if(p.next.val==val)
                p.next=p.next.next;
            else
                p=p.next;
        }
        return head;
        
    }
}
原文地址:https://www.cnblogs.com/qiaomu/p/4469968.html