[leedcode]Remove Linked List Elements
1906 ワード
/**
* 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;
}
}