[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 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...
随机推荐
- 渗透测试-基于白名单执行payload--Compiler
复现亮神课程 0x01 Compiler前言 说明:Microsoft.Workflow.Comiler.exe是.NET Framework默认自带的一个实用工具,用户能够以XOML工作流文件的形式 ...
- PHP 插入排序 -- 希尔排序
1.希尔排序 -- Shell Insertion Sort 时间复杂度:数学家正在勤劳的探索! 适用条件: 直接插入排序的改进,主要针对移动次数的减少,这取决于"增量队列"的取值 ...
- Google研发面试题
今天很开心找到了一个很不错的研发面试题,很考验临时反应能力,特地拿出来和大家分享一下此题以及自己做该题的心得体会!!! 题目: 总共有12个外表都一样的金属球,其中有11个球的重量是相同的,1个球的重 ...
- java集合之Vector向量基础
Vector向量: vector类似动态数组,向量和数组类似,但是数组容量一旦确定不可更改,而向量的容量可变.向量只可以保存任何类型对象且容量不限制,数组对元素类型无限制但是容量有限. 适用场合:向量 ...
- 《利用Python进行数据分析·第2版》第四章 Numpy基础:数组和矢量计算
<利用Python进行数据分析·第2版>第四章 Numpy基础:数组和矢量计算 numpy高效处理大数组的数据原因: numpy是在一个连续的内存块中存储数据,独立于其他python内置对 ...
- 一文理解java对象初始化顺序
例子 Talk is cheap, Show you the code! public class ParentClass { static int parentStaticField = 1; ...
- zookeeper - 通过java代码连接zookeeper(2)
首先创建一个Maven项目 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- 5.分析snkrs的Android的DeviceID生产规则
分析Android的DeviceID生产 前面已经把web端的分析了一些,要想实现其实容易也难,容易是规则很容易,难是时间生成控制很难,我之前大概花了一周时间分析和梳理,然后行为测试,之前我已经讲过c ...
- JdbcTemplate增删改
(1)Accountsdao层 //删除单个账户 int delaccount(Integer accountid); //添加单个用户 int addaccount(Accounts account ...
- Andriod开发环境搭建
一.相关安装文件准备