Remove Linked List Elements

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

Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeElements(ListNode head, int val) { ListNode fakehead = new ListNode(-1);
fakehead.next = head;
ListNode n = fakehead; if(head == null){
return head;
}
while(n.next != null){
if(n.next.val == val){
n.next = n.next.next;
}else{
n = n.next;
}
}
return fakehead.next;
}
}

LeetCode 203的更多相关文章

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

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

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

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

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

  4. Java实现 LeetCode 203 移除链表元素

    203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2 ...

  5. [LeetCode] 203. 移除链表元素(链表基本操作-删除)、876. 链表的中间结点(链表基本操作-找中间结点)

    题目 203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 题解 删除结点:要注意虚拟头节点. 代码 class Solution { public ListNode removeEle ...

  6. Leetcode 203 Remove Linked List Elements 链表

    去掉链表中相应的元素值 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...

  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. 将“Cocos2dx-截屏并设置图片尺寸 ”中cocos2d-x代码转换为2.2的代码

    Cocos2dx-截屏并设置图片尺寸: http://www.cocos2dev.com/?p=522 2.2 代码如下: void HelloWorld::screenShoot() { CCSiz ...

  2. HDU 1142 A Walk Through the Forest (求最短路条数)

    A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...

  3. UVA 10054 The Necklace(欧拉回路,打印路径)

    题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. 嵌入式LINUX入门到实践(二)

    这篇中将围绕韦东山LINUX第二部分教程源码,对IIC协议进行程序实现上的分析. /* I2C registers */#define IICCON      (*(volatile unsigned ...

  5. HDU 5773 The All-purpose Zero (变形LIS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5773 0可以改变成任何数,问你严格递增的子序列最长是多少. 猜测0一定在最长上升子序列中用到,比如2 ...

  6. SQLServer2008设置开启INTERNET远程连接

    SQL Server 2008默认是不允许远程连接的,sa帐户默认禁用的,如果想要在本地用SSMS连接远程服务器上的SQL Server 2008,需要做两个部分的配置: 使用sa账户登录SQL Se ...

  7. opennebula 编译日志

    [root@localhost opennebula-]# scons mysql=yes scons: Reading SConscript files ... Testing recipe: xm ...

  8. ecshop收货人信息中修改手机号为必填

    Ecshop 修改收货人信息 把电话改成选择填写 手机改为必填 (加强版) 1.  编辑根目录/js/utils.js 增加手机号码的正则表达式 参照Utils.isTel = function ( ...

  9. DX相机变换矩阵推导

    网上很多的推导过程都是错的,所以写一个. 先平移,再旋转就可以,先平移的原因是,如果先旋转的话,坐标系已经改了,所以先平移. 平移的变换和相机的变换是相反的,所以是: 平移完成后,相机的位置就和原点的 ...

  10. C#中Action和Func的使用

    在日常使用delegate时,我们通常需要显示声明一个名为XXX的委托,而在使用Action委托时,不必显示定义一个封装无参数过程的委托. 比如正常使用delegate: using System; ...