删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

直接上代码:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution
{
public ListNode removeElements(ListNode head, int val)
{
while(head!=null&&head.val==val)//初始化
{
ListNode pre=head;
head=pre.next;
pre.next=null;
}
if(head==null)//头节点为空
{
return null;
}
ListNode pre=head;
while(pre.next!=null)
{
ListNode cur=pre.next;
if(cur.val==val)//移除节点
{
pre.next=cur.next;
cur.next=null;
}
else //指针后移
{
pre=pre.next;
}
}
return head;
}
}

遍历链表,找出每个待删除节点前的每一个节点。

特殊情况:第一个节点就是待删除节点,要进行单独的操作。

注意点:当输入1->1时,删除完第一个节点,剩下的链表的头节点又是待删除节点。

LeetCode 203——移除链表(JAVA)的更多相关文章

  1. Java实现 LeetCode 203 移除链表元素

    203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2 ...

  2. [LeetCode] 203. 移除链表元素(链表基本操作-删除)、876. 链表的中间结点(链表基本操作-找中间结点)

    题目 203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 题解 删除结点:要注意虚拟头节点. 代码 class Solution { public ListNode removeEle ...

  3. [LeetCode] 203. 移除链表元素

    题目链接:https://leetcode-cn.com/problems/remove-linked-list-elements/ 题目描述: 删除链表中等于给定值 val 的所有节点. 示例: 输 ...

  4. 【LeetCode】203.移除链表元素

    203.移除链表元素 知识点:链表:双指针 题目描述 给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 . 示例 ...

  5. 力扣(LeetCode)移除链表元素 个人题解

    删除链表中等于给定值 val 的所有节点. 这题粗看并不困难,链表的特性让移除元素特别轻松,只用遇到和val相同的就跳过,将指针指向下一个,以此类推. 但是,一个比较麻烦的问题是,当链表所有元素都和v ...

  6. Leecode刷题之旅-C语言/python-203移除链表元素

    /* * @lc app=leetcode.cn id=203 lang=c * * [203] 移除链表元素 * * https://leetcode-cn.com/problems/remove- ...

  7. LeetCode通关:听说链表是门槛,这就抬脚跨门而入

    分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master       https://github.com/ch ...

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

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

  9. [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

随机推荐

  1. 创建express项目(nodejs)

    1.下载nodejs安装包 nodejs官网下载最新版本就行,网址:http://nodejs.cn/download/,点击自己适用的系统,自动下载跟电脑操作系统位数符合的安装包, 2.配置环境 最 ...

  2. Linux的简单命令(防火墙篇)

    名称 解释 重启 reboot 关机 shutdown  -h   now poweroff 查看本机IP地址 ifconfig 查看默认网卡信息的文件 cat /etc/sysconfig/netw ...

  3. shell 拾遗

    1, 按照行读取文件 while read line do echo ${line} done < ${filename} 2.循环中使用命令输出 while read line do echo ...

  4. 编译安装 Nginx

    一.下载 https://nginx.org/en/download.html yum install -y wget wget http://nginx.org/download/nginx-1.1 ...

  5. Spring对象依赖关系

    Spring中,如何给对象的属性赋值?  [DI, 依赖注入] 1) 通过构造函数 2) 通过set方法给属性注入值 3) p名称空间   4)自动装配(了解) 5) 注解 package loade ...

  6. .net 分布式锁实现

    摘要: .net分布式锁,包括redis分布式锁和zookeeper分布式锁的.net实现. 分布式锁在解决分布式环境下的业务一致性是非常有用的. 分布式锁 经常用于在解决分布式环境下的业务一致性和协 ...

  7. Java SpringBoot React Redux

    1.字符串转换相关 - React 前端JS部分 JSON.parse(JSON.stringify(copyRow)); 2.字符串分隔相关,弹出confirm确认框,显示换行信息 - React ...

  8. python 实例方法、类方法和静态方法

    #!/usr/bin/env python3.6 #-*- coding:utf-8 -*- # class Person(object): city = 'Beijing' def __init__ ...

  9. java安装配置

    1.下载 https://www.oracle.com/technetwork/java/javase/downloads/index.html 2.配置环境变量 点击"新建" 变 ...

  10. Expecting "jsp:param" standard action with "name" and "value" attributes

    浏览器访问报如下jsp标签错误: 根据提示,定位到jsp页面124行,代码如下: 查找原因,当<jsp:include></jsp:include>标签中没有参数时,不允许有空 ...