(LinkedList) 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) {
while(head!=null && head.val==val)
head=head.next;
if(head==null)
return head;
ListNode cur=head;
while(cur!=null && cur.next!=null){
if(cur.next.val==val){
cur.next=cur.next.next;
}
else
cur=cur.next;
}
return head;
}
}
(LinkedList) Remove Linked List Elements的更多相关文章
- (LeetCode 203)Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- [LintCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Have you met this question i ...
- Leetcode-203 Remove Linked List Elements
#203. Remove Linked List Elements Remove all elements from a linked list of integers that have val ...
- 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements
237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...
- 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题要求 ...
- 【LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...
- LeetCode Remove Linked List Elements 删除链表元素
题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...
- LeetCode_203. Remove Linked List Elements
203. Remove Linked List Elements Easy Remove all elements from a linked list of integers that have v ...
随机推荐
- [css3]圆盘旋转动画
效果:打开只能看到logo,鼠标放上去,圆盘渐显放大旋转展示出来 知识点: [html+css] 1.logo水平垂直居中于圆盘内,用到的样式 position: absolute; left: 0; ...
- 我原来忽略的web开发点
打开一个网页,看到的东西的背后还有看不见的东西,程序员通常在一个页面影藏了许多标签,这个页面可以用来在许多地方使用,因为模板相同,只是有点地方不一样.还有类似于新浪微博的页面使用了很多花样,消息推送( ...
- 关于ADDED_TO_STAGE事件
可视类初始化的时候,很多时候要用到stage属性,则要使用Event.ADDED_TO_STAGE事件,这个swf被其它的文件加载,如果直接在初始化函数内使用stage属性 .但是,文档类初始化函数内 ...
- Android_Layout (一)
layout (布局) --->Android 有五大布局,分别是: LinearLayout : 线性布局,子组件按照垂直或者水平方向来布局. RelativeLayout :相对布局,按照 ...
- JavaScript基础--事件驱动和访问CSS技术(十)
1.原理: 2.快速入门案例 js中的事件主要分为4种: 案例:监听鼠标点击事件,并能够显示鼠标点击的位置x,y <script language="javascript" ...
- android基础(二)Broadcast Receiver知识
1.广播的注册 (1)动态广播注册: 优点:可以灵活控制广播的注册与撤销 缺点:必须在程序启动时才能接收广播 IntentFilter :当Intent在组建之间传递时,组件想告诉android系统自 ...
- 2016 - 1 - 23 json解析
一: json 1. 什么是json 1.1 json是一种轻量级的数据格式,一般用于数据交互. 1.2 服务器返回给客户端的数据,一般都是JSON或者XML格式(文件下载除外). 2. JS ...
- 大量无线键盘存在KeySniffer漏洞-可嗅探用户输入的内容
几美元的一根天线.一个无线发射器,还有几行Python代码,有了这些,黑客就可以在几十米开外主动地记录下你的用户名.密码.信用卡.你写的稿子,总之就是你用无线键盘输入的任何东西. 黑客所利用的是一种无 ...
- Javascript笔记一
Javascript: ECMAscript :相当于翻译器 翻译电脑于代码 解释器 DOM document object model 文档 对象 模型 --->document 获取 ...
- Caffe 源碼閱讀(三) caffe.cpp
补:主要函数运行顺序: main>>GetBrewFunction>>train>>Solve 從main函數說起: 1.gflags庫中爲main函數設置usag ...