[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 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...
随机推荐
- Python_函数做字典的值
当需要用到3个及以上的if...elif...else时就要考虑该方法进行简化 通过将函数名称当做字典的值,利用字典的关键字查询,可以快速定位函数,进行执行 [场景]用户查询信息,输入fn查询,执行对 ...
- thinkphp5框架之请求
又看到请求这一部分,个人认为这部分是算重要的一部分 单独记一篇笔记. 0x01 request请求对象 如果要获取当前的请求信息,可以使用\think\Request类,完全开发手册中也有提到,继承系 ...
- swoole与php协程实现异步非阻塞IO开发
“协程可以在遇到阻塞的时候中断主动让渡资源,调度程序选择其他的协程运行.从而实现非阻塞IO” 然而php是不支持原生协程的,遇到阻塞时如不交由异步进程来执行是没有任何意义的,代码还是同步执行的,如下所 ...
- 一张图看懂Rxjava的原理
前言 Rxjava是NetFlix出品的Java框架, 官方描述为 a library for composing asynchronous and event-based programs usin ...
- TCP Traffic Analyzer
TCP Traffic Analyzer 工具yahoo 发布的一款开源网络分析工具,可以分析网络应用在服务器端与客户端之间的运行状态Yconalyzer保持与tcpdump兼容,两者生成的抓取文件能 ...
- TensorFlow2.0(9):TensorBoard可视化
.caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...
- Cocos Creator实现1024游戏,免费提供代码。
效果预览 获取代码 私信或留言. 游戏介绍 ● Github上的代码,不能进行合并操作,修改以后,功能类似2048,空白块赏随机位置,生成2,可以往左.右.上.下滑动,数字会朝着指定方向运动,相 ...
- java-反编译工具(luyten)
下载地址:https://github.com/deathmarine/Luyten
- APP打包设置程序版本号
正确设置方式是: 注意,以下修改不会起作用<manifestxmlns:android="http://schemas.android.com/apk/res/android" ...
- fenby C语言 P19
#include <stdio.h> int main(){ int i,j; for(i=1;i<=8;i++) { for(j=1;j<=i;j++) { printf(& ...