删除链表中等于给定值 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. Error creating bean with name 'persistenceExceptionTranslationPostProcessor' defined in class path resource [org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfiguration

    dubbo 包和SpringBoot 冲突,注释就可以正常启动

  2. quartz中的corn表达式

    一个Quartz的CronTrigger表达式分为七项子表达式,其中每一项以空格隔开,从左到右分别是:秒,分,时,月的某天,月,星期的某天,年:其中年不是必须的,也就是说任何一个表达式最少需要六项! ...

  3. 【零基础】为什么Facebook发币就不一样

    参考: https://baijiahao.baidu.com/s?id=1637182596912694597&wfr=spider&for=pc https://blog.csdn ...

  4. mybatis sql不等于

    转载地址:    https://blog.csdn.net/weixin_40797576/article/details/78796028 select * from test where id& ...

  5. ctf密码学------密文解码python脚本(凯撒解密)

    题目来源实验吧 分析题意,说是困在栅栏中,所以将字符栅栏解密看看有什么,利用工具CTFcraktools 得到三条密文 然后说是密码是凯撒,在将四栏依次凯撒解码,寻找可能的key,这里很显然,在尝试第 ...

  6. Qt编写数据可视化大屏界面电子看板2-配色方案

    一.前言 做完整个数据可视化大屏界面电子看板系统后,为了提升点逼格,需要提供好几套默认的风格样式以供选择,这样用户可以选择自己喜欢的配色方案来作为整个系统的颜色方案,去看了下市面上大部分的大屏电子看板 ...

  7. WebServer_参考

    参考:http://blog.csdn.net/cjsafty/article/details/9323425 这里顺便记录下几个页面      lajphttps://code.google.com ...

  8. 前端构建工具gulp使用 (转)

    http://www.cnblogs.com/starof/p/5194622.html 前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 ...

  9. 在DELPHI中动态创建控件以及控件的事件

    在DELPHI中我们经常要动态的创建控件以及控件的事件.例如,我们可能想根据程序需要动态的创建一些Tshape组件来创建某个图形,并使得在鼠标移动上去之后可以完成某些操作.这一般需要需要三步: 生成一 ...

  10. rsync重启的shell脚本

    https://zhidao.baidu.com/question/2078634436717887028.html