思路:
题解:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode l0 = new ListNode(0);
l0.next = head;
ListNode prev = l0,curr = head;
while(curr != null){
if(curr.val == val){
prev.next = curr.next;
curr = curr.next;
}else{
prev = curr;
curr = curr.next;
}
}
return l0.next;
}
}