删除链表中的节点
删除链表中等于给定值 val 的所有节点。
示例:
1 2
|
输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4->5
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
class { public ListNode removeElements(ListNode head, int val) { if (head == null) return head; ListNode current = head; ListNode future = head.next; while (future != null) { if (future.val == val) { current.next = future.next; future = future.next; } else { current = current.next; future = future.next; } }
if (head.val == val) head = head.next; return head; } }
|
近期评论