①英文题目

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 (移除链表元素)(链表)的更多相关文章

  1. [LintCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Have you met this question i ...

  2. [LeetCode] 203. Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  3. LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java

    Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...

  4. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  5. LeetCode OJ :Remove Linked List Elements (移除链表元素)

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  6. LeetCode算法题-Remove Linked List Elements(Java实现)

    这是悦乐书的第189次更新,第191篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第48题(顺位题号是203).移除单链表中节点值为val的节点.例如: 输入:1-> ...

  7. (LeetCode 203)Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  8. [LC]83题 Remove Duplicates from Sorted List(删除排序链表中的重复元素)(链表)

    ①英文题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exa ...

  9. 203. Remove Linked List Elements - LeetCode

    Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...

随机推荐

  1. Math中ceil中为什么会有负零

    double c=Math.ceil(-0.5); double d=Math.floor(0.5); System.out.println(c); System.out.println(d); Sy ...

  2. std::shared_future/future

    std::future提供了一种访问异步操作结果的机制.

  3. openssl生成密钥/证书

    一.公钥/私钥/签名/验证签名/加密/解密/非对称加密 对称加密:用同一个密码  加密/解密  文件. 非对称加密:加密用的一个密码,解密用另外一组密码. 加密解密:公钥加密数据,然后私钥解密. 公钥 ...

  4. c++::Mysql::ORM 开发环境搭建

    官网地址:https://www.codesynthesis.com/products/odb/ 环境搭建:ubuntu16.04-64 1.安装mysqlClient sudo apt-get in ...

  5. pytest3-命令行选项

    1.pytest -h 查看pytest常用命令 E:\myproj\pytest_demo>pytest -h usage: pytest [options] [file_or_dir] [f ...

  6. linux+jenkins+github+python持续集成

    1.服务器上事先安装jenkins   参见:linux上war包方式安装Jenkins 2.新建一个自由风格的job,名字随意起 3.配置git(拉取github代码) 从github复制pytho ...

  7. Activity 学习(二) 搭建第一个Activity流程框架

    本次示例使用的IDER测试完成 测试背景 : xx饿了去饭店吃饭  需要先和服务员点餐  点完餐后服务员将菜品传递给厨师制作  制作完成后吃饱 一 :创建流程图 创建上一篇测试成功出现的BpmnFil ...

  8. kmp算法,求重复字符串

    public class Demo { public static void main(String[] args) { String s1 = "ADBCFHABESCACDABCDABC ...

  9. 【Java必修课】四类方法删除List里面的所有null值

    1 简介 万恶的null已经折磨程序员许久了,也带来了许多难以发现却造成严重损失的NullPointerException.我们需要尽可能的避免它,有一种简单的办法就是在它进入下轮处理前,我们就把它扼 ...

  10. Java基础(三十二)JDBC(2)连接数据库

    一.连接数据库的过程 连接数据库的过程:加载数据库驱动程序,不过只需在第一次访问数据库时加载一次,然后在每次访问数据库时创建一个Connection实例,然后执行操作数据库的SQL语句,并返回执行结果 ...