①英文题目

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. javascript 对象和字符串互转

    Object  =>  String : console.log(JSON.stringify(e)); String => Object : JSON.parse(str)

  2. Ubuntu 安装中文

    系统环境:

  3. 《锋利的jQuery》学习总结

    通过对<锋利的jQuery>(第二版)一书的学习,发现此书讲解通俗易懂,是学习jQuery的一本很好的指导书,特作如下总结.此书主要讲解了jQuery的常用操作,包括认识jQuery,jQ ...

  4. opencv实践::直线检测

    问题描述 寻找英语试卷填空题的下划线,这个对后期的切图与自动 识别都比较重要. 解决思路 方法: 通过图像形态学操作来寻找直线,霍夫获取位置信息与显示. #include <opencv2/op ...

  5. Java中package与import

    使用实例: package 一般来说,package语句必须作为源文件的第一条非注释性语句.一个java源文件只能指定一个包,即只能包含一条package语句,该源文件中可以定义多个类,则这些类将全部 ...

  6. vue实现简单学生信息管理案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 【RabbitMQ 实战指南】一 延迟队列

    1.什么是延迟队列 延迟队列中存储延迟消息,延迟消息是指当消息被发送到队列中不会立即消费,而是等待一段时间后再消费该消息. 延迟队列很多应用场景,一个典型的应用场景是订单未支付超时取消,用户下单之后3 ...

  8. Xbim.GLTF源码解析(三):Builder类

    原创作者:flowell,转载请标明出处:https://www.cnblogs.com/flowell/p/10838706.html IFC提取转换成GLTF的逻辑在Builder类中, Buil ...

  9. spring cloud 面试题总结

    前言,随着分布式的时代到来,现在微服务越来越火了,Spring Cloud已经成为一个面试必问的考点,下面我们就Cloud这一些列的组件来一个入门到面试的过程.开篇前,先让大家看几个常见的Spring ...

  10. linux IMX6 汇编点亮一个LED灯

    驱动Linux引脚与驱动STM32其实是一样的,都是在操作寄存器,在相应的寄存器上附上相应的值即可驱动. IMX6U手册上有各个管脚的命名,跟STM32不同,IOMUXC_SW_MUC_CTL_PAD ...