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. ABP安装,应用及二次开发视频

    CSDN课程:http://edu.csdn.net/lecturer/944

  2. 2019OO第二单元作业总结

    OO第二单元的作业主题是模拟电梯. ---------------------------------------------------------------------------------- ...

  3. Javascript 4.3 事件处理函数

    鼠标指针悬停在某个元素上时触发一个动作:onmouseover事件处理函数 鼠标指针离开某个元素时触发一个动作:onmouseout事件处理函数 点击某个链接时触发一个动作:onclick事件处理函数 ...

  4. C语言 基础

    内存的定义 在学习python的时候 了解过内存的管理机制,例如引用计数,垃圾回收,小内存池的概念. 但是并不了解内存究竟是个什么东西?不了解内存的实际存储方式. Mac OS Mojave 处理器 ...

  5. git 与 ftp 共同工作

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

  6. vs单独调试dll

    用生成好的exe单独调试dll,右键项目属性->调试->命令->选择exe

  7. docker容器以ROOT账号登录(获取ROOT权限/ROOT密码)

    第一步:查看容器的CONTAINER ID docker ps 第二步:获取root权限,例如需要进入的CONTAINER ID为4650e8d1bcca docker exec -ti -u roo ...

  8. 工控随笔_14_西门子_Step7项目:打开项目不可用解决方法

    由于计算机系统区域和语言的设置,以及Step建立项目时的不同设置,有时候利用Step7打开项目时 会遇到如下情况:   项目不可用. 具体如下图所示: 图 step 7 打开时项目不可用 一.Step ...

  9. 解决WIN7第一次开机冷启动QQ未响应的办法

    为什么WIN7第一次开机冷启动QQ未响应?WIN10就没事? http://bbs.wuyou.net/forum.php?mod=viewthread&tid=409516&extr ...

  10. python3学习笔记11(函数)

    函数 python提供了许多内建函数,例如print(). 自己创建的函数,叫做用户自定义函数. 定义一个由自己想要功能的函数,以下是简单的规则: 函数代码块以 def 关键词开头,后接函数标识符名称 ...