给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
    ebcd1308bd7689956264bea16dafe38.png
    输入:head = [1,2,6,3,4,5,6], val = 6
    输出:[1,2,3,4,5]

    示例 2:
    输入:head = [], val = 1
    输出:[]

    示例 3:
    输入:head = [7,7,7,7], val = 7
    输出:[]

    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode() {}
    7. * ListNode(int val) { this.val = val; }
    8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    9. * }
    10. */
    11. class Solution {
    12. public ListNode removeElements(ListNode head, int val) {
    13. ListNode dummy = new ListNode(0);
    14. dummy.next = head;
    15. ListNode prev = dummy;
    16. while(head != null){
    17. if(head.val == val){
    18. prev.next = head.next;
    19. head = head.next;
    20. }else{
    21. prev = head;
    22. head = head.next;
    23. }
    24. }
    25. return dummy.next;
    26. }
    27. }