删除链表中等于给定值 val 的所有元素。
示例
给定: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
返回: 1 --> 2 --> 3 --> 4 --> 5

详见:https://leetcode.com/problems/remove-linked-list-elements/description/

Java实现:

/**
* 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) {
if(head==null){
return null;
}
ListNode first=new ListNode(-1);
first.next=head;
ListNode cur=first;
while(cur.next!=null){
if(cur.next.val==val){
cur.next=cur.next.next;
}else{
cur=cur.next;
}
}
return first.next;
}
}

203 Remove Linked List Elements 删除链表中的元素的更多相关文章

  1. LeetCode Remove Linked List Elements 删除链表元素

    题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...

  2. LeetCode Delete Node in a Linked List (删除链表中的元素)

    题意:给一个将要删除的位置的指针,要删除掉该元素.被删元素不会是链尾(不可能删得掉). 思路:将要找到前面的指针是不可能了,但是可以将后面的元素往前移1位,再删除最后一个元素. /** * Defin ...

  3. 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题要求 ...

  4. 203. Remove Linked List Elements - LeetCode

    Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...

  5. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  6. LintCode之删除链表中的元素

    题目描述 我的代码 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode n ...

  7. 【LeetCode】203. Remove Linked List Elements

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

  8. 【刷题-LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...

  9. lintcode:删除链表中指定元素

    题目 删除链表中等于给定值val的所有节点. 样例 给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1-> ...

随机推荐

  1. 国内博客(blog)搬家工具(服务)大全

    如今网络上的博客搬家 服务,博客搬家工具 越来越多,博客联盟 大概收集了下,希望对那些想搬家的博客有所帮助. 一.和讯博客的“搬家公司”提供博客搬家 服务 搬家服务地址:点这里 目标对象:新浪博客 . ...

  2. Android签名机制之---签名验证过程具体解释

    一.前言 今天是元旦,也是Single Dog的嚎叫之日,仅仅能写博客来祛除寂寞了,今天我们继续来看一下Android中的签名机制的姊妹篇:Android中是怎样验证一个Apk的签名. 在前一篇文章中 ...

  3. Android Material Design-Maintaining Compatibility(保持兼容性)-(七)

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/40634829 翻译自: http://developer.android.com/traini ...

  4. JAVA 并发编程-返回运行结果(Callable和Future)(九)

    启动一个线程不论使用Thread或者Runnable的时候.都是没有返回结果的. 也就是说Thread和Runnable的run()方法必须没有返回值. public void run(){} 解决方 ...

  5. Win32 Windows编程 七

    定时器消息 1. WM_TIMER 依照定时器设置的时间段,自己主动向窗体发送一个定时器消息WM_TIMER.优先级比較低 定时器精度比較低.毫秒级别.消息产生时间也精度比較低 2 .消息和函数 WM ...

  6. Chapter1-data access reloaded:Entity Framework(上)

    本章包括以下几个部分: 1.DataSet and classic ADO.NET approach2.Object model approach3.Object/relational mismatc ...

  7. 2016/3/27 分页 共X条数据 本页x条 本页从x-y条 x/y页 首页 上一页 123456 下一页 末页 pagego echo $page->fpage(7,6,5,4,3,2,1,0);

    显示效果: fpage.class.php <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; / ...

  8. JavaScript的高大强

    1,JavaScript的引入方式 1.1>Script标签内写代码 <Script> //这里写JS代码的地方 </Script> 1.2>引入额外的JS文件 & ...

  9. centos7和redhat7的比特币环境搭建

    比特币版本是 bitcoin-0.12 问题1: [root@localhost bitcoin-master]# ./autogen.sh  which: no autoreconf in (/us ...

  10. cassandra删除所有数据,重置为初始状态——删除<data dir>/data/* <data dir>/commitlog/* <data dir>/saved_caches/* 重启cassandra即可

    Are you looking for a method other than drop keyspace? Okay based on your clarification... I would s ...