/**
* 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 first = new ListNode(0);
first.next = head;
ListNode pre = first, cur = head, nxt = cur.next;
while(cur!=null){
if(cur.val==val){
cur = nxt;
if(cur!=null) nxt = cur.next;
pre.next = cur;
}else{
pre = cur;
cur = nxt;
if(cur!=null) nxt = cur.next;
}
}
return first.next;
}
}
* 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 first = new ListNode(0);
first.next = head;
ListNode pre = first, cur = head, nxt = cur.next;
while(cur!=null){
if(cur.val==val){
cur = nxt;
if(cur!=null) nxt = cur.next;
pre.next = cur;
}else{
pre = cur;
cur = nxt;
if(cur!=null) nxt = cur.next;
}
}
return first.next;
}
}