Description
Remove all elements from a linked list of integers that have value val.
Example
1 2
| Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5
|
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class Solution { public ListNode removeElements(ListNode head, int val) { if (head == null) return head; ListNode dummy = new ListNode(0); dummy.next = head; ListNode cur = head; ListNode pre = dummy; while(cur != null){ if (cur.val == val) pre.next = cur.next; else pre = cur; cur = cur.next; } return dummy.next; } }
|