题意:删除链表中等于给定值 val 的所有节点。 示例 1: 输入: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 输出:[] 思路:删除的时候要找到前一个元素,所以为了方便引入虚拟头结点进行遍历。 class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dyNode = new ListNode(-1); dyNode.next = head; ListNode cur = dyNode; while(cur.next!=null){ if(cur.next.val==val){ cur.next=cur.next.next; }else{ cur = cur.next; } } return dyNode.next; } } 你可以选择使用单链表或者双链表,设计并实现自己的链表。...
Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile