题意:删除链表中等于给定值 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
Articles by ListNode temp
代码随想录算法训练营Day4 | | 24. 两两交换链表中的节点 ,19.删除链表的倒数第N个节点 , 面试题 02.07. 链表相交
24. 两两交换链表中的节点 力扣题目链接(opens new window) 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 这道题目正常模拟就可以了。 建议使用虚拟头结点,这样会方便很多,要不然每次针对头结点(没有前一个指针指向头结点),还要单独处理。 对虚拟头结点的操作,还不熟悉的话,可以看这篇链表:听说用虚拟头节点会方便很多? (opens new window)。 接下来就是交换相邻两个元素了,此时一定要画图,不画图,操作多个指针很容易乱,而且要操作的先后顺序 初始时,cur指向虚拟头结点,然后进行如下三步: 操作之后,链表如下: 看这个可能就更直观一些了: /** * Definition for singly-linked list.
Week1题目重刷_peach2580的博客-CSDN博客
今天把week1的题目都重新刷了一遍,明天开始week2的内容~ 704.二分查找 class Solution { public int search(int[] nums, int target) { int l = 0, r = nums.length - 1, m; while (l <= r) { m = (l + r) >>> 1; if (nums[m] < target) { l = m + 1; } else if (nums[m] > target) { r = m - 1; } else { return m; } } return -1; } } class Solution { public int search(int[] nums, int target) { int l = 0, r = nums.length, m; while (l < r) { m = (l + r) >>> 1; if (nums[m] < target) { l = m + 1; } else if (nums[m] > target) { r = m; } else {...
Show More
loading
Actions
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.Get in touch with ListNode
Contact ListNode, search articles and posts on X, monitor coverage, and track replies from one place.
Learn more about Muck Rack