[LC]203题 Remove Linked List Elements (移除链表元素)(链表)
①英文题目
Remove all elements from a linked list of integers that have value val.
Example:
Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
②中文题目
删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
③思路
1、加入链表为空,那么直接返回head。
2、考虑6,5,4,3,2,1,val=6.
所以,一开始就要判断头结点的值是否等于val。所以考虑用while或者if来写此处逻辑。
3、考虑,6,6,6,5,4,3,2,1,val=6
当我删除第一个6之后,后面又来一个6,所以,这个需要不停循环,直到首个元素不再是6.
所以大致是
while(head!=null&&head.val==val){
head=head.next;
}
4、考虑 1->2->6->3->4->5->6, val = 6。
国际惯例,先让head赋给别的结点名,比如
ListNode pre=head;//以后有什么事情,就在pre上做操作。而要return全部时,就return head
判断,如果pre.next的val和pre.next.next的val相等,那就把原本的pre.next删掉,代码写起来就是让pre的next不再指向pre.next,而是指向pre.next.next,
这个逻辑需要用一个while循环来写,while里保证pre.next存在,即不等于null。代码写起来就是
while(pre.next!=null){
if(pre.next.val==pre.next.next.val)
pre.next==pre.next.next; //看见一次next,就读作往后走了一个单位。
else
pre=pre.next; //否则,让pre往后顺延一个单位
}
④总的代码如下:
class Solution {
public ListNode removeElements(ListNode head, int val) {
while(head!=null&&head.val==val)
head=head.next; //只有当头结点的val不再等于val时,才走出循环
if(head==null)
return head;
ListNode pre=head;
while(pre.next!=null){
if(pre.next.val==val)
pre.next=pre.next.next;
else
pre=pre.next; //否则,让pre往后顺延1个单位
}
return head;
}
}
⑤
1、第一次见到连续两个.next;
2、几乎看见每一个链表题里,都会写一句把原本的头结点给赋给一个新声明的结点,比如ListNode pre=head;
当然,在这个赋值语句之前,先要保证head已经稳定了(比如本题总程序的3-4行就是在寻求一个稳定的head,不然,谁敢把不确定的head赋值给pre)。
⑥别人的“递归”代码:
public ListNode removeElements(ListNode head, int val) {
if (head == null)
return null;
head.next = removeElements(head.next, val);//先不管head.val是否等于val,而是先保证从第二个元素开始,去掉每个等于val的元素。
if (head.val == val) {
return head.next;
} else {
return head;
}
}
[LC]203题 Remove Linked List Elements (移除链表元素)(链表)的更多相关文章
- [LintCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Have you met this question i ...
- [LeetCode] 203. Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java
Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- LeetCode OJ :Remove Linked List Elements (移除链表元素)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- LeetCode算法题-Remove Linked List Elements(Java实现)
这是悦乐书的第189次更新,第191篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第48题(顺位题号是203).移除单链表中节点值为val的节点.例如: 输入:1-> ...
- (LeetCode 203)Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- [LC]83题 Remove Duplicates from Sorted List(删除排序链表中的重复元素)(链表)
①英文题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exa ...
- 203. Remove Linked List Elements - LeetCode
Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...
随机推荐
- PMP(第六版)中的各种矩阵表格
- docker-compose 的使用
1.安装docker-compose,参考官方教程:https://docs.docker.com/compose/install/ [chenjl@ipha-dev71- ~]$ sudo curl ...
- jmeter-使用代理服务器录制脚本
使用代理服务器录制脚本 1.测试计划-添加线程组 2.工作台添加HTTP代理服务器(路径:工作台-右键添加-非测试元件-HTTP代理服务器) 3.端口号一般由8888改为其他的 4.打开chrome的 ...
- python编程系列---最详细的讲解进程与线程的关系
进程与线程 先引入三个比如: cpu---公司 进程---办公室 线程---程序员(我们) 全局变量,内存等资源---公司提供的电脑,桌子等 进程: 操作系统分配程序执行资源的单位 线程:进 ...
- find命令面试题
注意 (1)建议先创建快照 (2)有可能存在命令正确,但是查找不到文件的情况,是因为不存在相关条件的文件 (3)如果存在命令正确,但是查找不到文件的情况,则先创建相关的文件.目录.用户.组,设置好对应 ...
- java架构之路-(Redis专题)简单聊聊redis分布式锁
这次我们来简单说说分布式锁,我记得过去我也过一篇JMM的内存一致性算法,就是说拿到锁的可以继续操作,没拿到的自旋等待. 思路与场景 我们在Zookeeper中提到过分布式锁,这里我们先用redis实现 ...
- MyBatis 示例-传递多个参数
映射器的主要元素: 本章介绍 select 元素中传递多个参数的处理方式. 测试类:com.yjw.demo.MulParametersTest 使用 Map 传递参数(不建议使用) 使用 MyBat ...
- 关于在vue-cli脚手架中使用CDN引入element-ui不成功的坑
在前端开发过程中,为了减少最后打包出来的体积,我们会用到cdn引入一些比较大的库来解决. 常见我们引入的element-ui库,在最近使用cdn引入时,无论如何都引入不成功,其他的如Vue.vue-r ...
- spring cloud(Greenwich SR)- Eureka
spring cloud study 本次学习基于spring cloud Greenwich SR1 版本 学习要点: Spring Boot/Spring Cloud应用开发套路 加依赖 加注解 ...
- Kubernetes入门学习--在Ubuntu16.0.4安装配置Minikube
目 录 一. 安装minikube环境 1.1. 安装前准备 1.2. 安装Lantern 1.2.1. Lantern下载网站 1.2.2. Lantern下载地址 1.2.3. Lantern安装 ...