Remove Linked List Elements

    Example

    题解

    1. /**
    2. * Definition for singly-linked list.
    3. * int val;
    4. * ListNode next;
    5. * ListNode(int x) { val = x; }
    6. * }
    7. */
    8. /**
    9. * @param head a ListNode
    10. * @param val an integer
    11. * @return a ListNode
    12. */
    13. public ListNode removeElements(ListNode head, int val) {
    14. dummy.next = head;
    15. ListNode curr = dummy;
    16. while (curr.next != null) {
    17. curr.next = curr.next.next;
    18. } else {
    19. curr = curr.next;
    20. }
    21. }
    22. return dummy.next;
    23. }