题目:
Remove all elements from a linked list of integers that have value val.
Example:
Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
public class RemoveLinkedListElements {
public ListNode removeElements(ListNode head, int val) {
if(head == null)
return head;
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode cur = dummyHead;
while(cur.next != null){
if (cur.next.val == val){
ListNode delNode = cur.next;
cur.next = delNode.next;
delNode.next = null;
}
else cur = cur.next;
}
return dummyHead.next;
}
}