删除链表中的指定元素。
/**
* 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) {
if(head == null)return null;
ListNode p,q;
p = head.next;
q = head;
while(p!=null) {
if(p.val == val) {
q.next = p.next;
p = p.next;
}
else {
p = p.next;
q = q.next;
}
}
if(head.val == val) {
head = head.next;
}
return head;
}
}