Remove all elements from a linked list of integers that have value val.

Example:

Input:  ->->->->->->, val =
Output: ->->->->

本题的思路很简单,就是单纯的删除链表元素操作,如果头结点的元素就是给定的val值,需要借助虚结点dummy去判断。

方法一:(C++) C++中注意结点的释放

 ListNode* removeElements(ListNode* head, int val) {
ListNode* dummy=new ListNode(-),* pre=dummy;
pre->next=head;
while(pre->next){
if(pre->next->val==val){
ListNode* t=pre->next;
pre->next=pre->next->next;
t->next=NULL;
delete t;
}
else
pre=pre->next;
}
return dummy->next;
}

Java:

 public ListNode removeElements(ListNode head, int val) {
if(head==null||(head.next==null&&head.val==val))
return null;
ListNode dummy=new ListNode(-1),cur=dummy;
cur.next=head;
while(cur.next!=null){
if(cur.next.val==val)
cur.next=cur.next.next;
else
cur=cur.next;
}
return dummy.next;
}

方法二:不使用虚结点的话,最后只需要再判断一下头结点的值是否符合要求(C++)

 ListNode* removeElements(ListNode* head, int val) {
if(head==NULL)
return head;
ListNode* cur=head;
while(cur->next){
if(cur->next->val==val)
cur->next=cur->next->next;
else
cur=cur->next;
}
return (head->val==val)?head->next:head;
}

LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java的更多相关文章

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

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

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

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

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

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

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

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

  5. leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)

    203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...

  6. LeetCode 203. Remove Linked List Elements (移除链表中的项)

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

  7. Java for LeetCode 203 Remove Linked List Elements

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

  8. (easy)LeetCode 203.Remove Linked List Elements

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

  9. Java [Leetcode 203]Remove Linked List Elements

    题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...

随机推荐

  1. Development descriptor

    部署描述符指的是配置文件对于一个假象部署到一些容器/发动机. 在Java平台,企业版部署描述符描述组件.模块或应用程序(例如web应用程序或者企业应用程序)应该被部署.它指导部署工具部署具有特定容器选 ...

  2. SQL注入学习(一)

    注入攻击的本质:web应用程序没有过滤用户输入或过滤不严谨,直接把用户输入的恶意数据当做代码执行 两个条件: 1.用户能够控制输入 2.原本程序要执行的代码,拼接了用户输入的数据 注入类型 SQL注入 ...

  3. Dubbo的三种连接方式

    1.采用zookeeper作为注册中心 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...

  4. 二进制中的个数(JAVA)

    二进制中的1的个数 题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 思路:用位运算来进行移1操作.(首先得知道数在计算机中都是以01来放置的) 1,若由一个数11100,当 ...

  5. Actifio中的Group和Consistency Group

    多个应用程序具有相同的保护需求时可以使用应用程序组: Groups用于简化管理,将策略应用于组内的应用程序. 组中的每个应用程序的备份映像单独执行装载,克隆和还原操作. Consistency Gro ...

  6. Centos7使用yum快速安装ansible

    ansible功能简介:ansible可以实现批量系统配置.批量软件部署.批量文件拷贝.批量运行命令等功能.主要基于ssh实现连接各个被控制端 yum默认安装的ansible,常用的配置文件有两个,一 ...

  7. Python Faker的使用(1):基础使用方法与函数速查,生成随机数据

    在软件需求.开发.测试过程中,有时候需要使用一些测试数据,针对这种情况,我们一般要么使用已有的系统数据,要么需要手动制造一些数据. 在手动制造数据的过程中,可能需要花费大量精力和工作量,现在好了,有一 ...

  8. orientdb docker-compose 运行

    orientdb 很早就跑过,但是现在在跑,发现配置有些变动,原有studio 直接就可以访问的,新版本的居然还需要自己添加 server 的配置,所以为了方便使用docker-compose 运行, ...

  9. Thing in java 第5章,初始化和清理,练习题答案

    /** * Created by Sandy.Liu on 2018/7/28. * Thinking in java version 4, chapter 5, practice 2 * Creat ...

  10. git 与 ftp 共同工作

    因git主要用于版本管理,代码同步方面,因临时调试等原因,需要使用ftp上传文件. 但因为git的账户为ubuntu,ftp是虚拟账户overlord 导致文件权限不同,出现的问题主要有: 1.ftp ...