LeetCode 203——移除链表(JAVA)
删除链表中等于给定值 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)的更多相关文章
- Java实现 LeetCode 203 移除链表元素
203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2 ...
- [LeetCode] 203. 移除链表元素(链表基本操作-删除)、876. 链表的中间结点(链表基本操作-找中间结点)
题目 203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 题解 删除结点:要注意虚拟头节点. 代码 class Solution { public ListNode removeEle ...
- [LeetCode] 203. 移除链表元素
题目链接:https://leetcode-cn.com/problems/remove-linked-list-elements/ 题目描述: 删除链表中等于给定值 val 的所有节点. 示例: 输 ...
- 【LeetCode】203.移除链表元素
203.移除链表元素 知识点:链表:双指针 题目描述 给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 . 示例 ...
- 力扣(LeetCode)移除链表元素 个人题解
删除链表中等于给定值 val 的所有节点. 这题粗看并不困难,链表的特性让移除元素特别轻松,只用遇到和val相同的就跳过,将指针指向下一个,以此类推. 但是,一个比较麻烦的问题是,当链表所有元素都和v ...
- Leecode刷题之旅-C语言/python-203移除链表元素
/* * @lc app=leetcode.cn id=203 lang=c * * [203] 移除链表元素 * * https://leetcode-cn.com/problems/remove- ...
- LeetCode通关:听说链表是门槛,这就抬脚跨门而入
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master https://github.com/ch ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- [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 ...
随机推荐
- CF1204B
CF1204B-Mislove Has Lost an Array 题意: 给你n,l,r 表示在区间1-n内至少有l个不相同的数至多有r个不相同的数,而且这些数不是1就是偶数而且每个偶数/2得到的数 ...
- 【分类模型评判指标 二】ROC曲线与AUC面积
转自:https://blog.csdn.net/Orange_Spotty_Cat/article/details/80499031 略有改动,仅供个人学习使用 简介 ROC曲线与AUC面积均是用来 ...
- Linux dd烧写系统
虽然用dd指令烧写系统很简单,但是久而久之忘得也太快了,赶紧整理一下. .img 系统镜像 .iso U盘启动引导文件 1. 先来查看本机磁盘情况,打开Linux终端界面(快捷键Ctrl + Alt ...
- Windows Server 2012R2 部署 Domain Controller
1. Create a machine as Domain Controller; 2. Change DNS server address as 127.0.0.1; 3. Change Compu ...
- 自动更新文件夹下所有DLL 至最新修改时间版本
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Linux下 安装jdk8
一.文件准备 1.1 文件名称 jdk-8u121-linux-x64.tar.gz 1.2 下载地址 http://www.oracle.com/technetwork/java/javase/do ...
- java.lang.reflect.Method.getAnnotation()方法示例
转: java.lang.reflect.Method.getAnnotation()方法示例 作者: 初生不惑 Java技术QQ群:227270512 / Linux QQ群:479429477 ...
- springmvc集成swagger
1.保证项目为maven项目 2.导入jar包依赖 <dependency> <groupId>io.springfox</groupId> <artifac ...
- Kafka管理与监控——调优
1.JVM参数配置优化 如果使用的CMS GC算法,建议JVM Heap不要太大,在4GB以内就可以.JVM太大,导致Major GC或者Full GC产生的“stop the world”时间过长, ...
- 等待数据库引擎恢复句柄失败 SqlServer2012安装时报错 Win10
上周,在一批Win10系统电脑上安装SqlServer 2012时,屡次发生报错,安装失败,显示的失败信息是:等待数据库引擎恢复句柄失败 如下图所示: 面对这样的错误,我的第一反应是百度,在百度上找了 ...