Remove all elements from a linked list of integers that have value val. Example Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5
Remove all elements from a linked list of integers that have value val.
val
Example
Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5
1->2->3->3->4->5->3
1->2->4->5
删除链表中等于给定值val的所有节点。
删除链表中指定值,找到其前一个节点即可,将 next 指向下一个节点即可
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { /** * @param head a ListNode * @param val an integer * @return a ListNode */ public ListNode removeElements(ListNode head, int val) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode curr = dummy; while (curr.next != null) { if (curr.next.val == val) { curr.next = curr.next.next; } else { curr = curr.next; } } return dummy.next; } }
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8