LeetCode 203
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的更多相关文章
- 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 all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- [LeetCode] 203. Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- Java实现 LeetCode 203 移除链表元素
203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2 ...
- [LeetCode] 203. 移除链表元素(链表基本操作-删除)、876. 链表的中间结点(链表基本操作-找中间结点)
题目 203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 题解 删除结点:要注意虚拟头节点. 代码 class Solution { public ListNode removeEle ...
- Leetcode 203 Remove Linked List Elements 链表
去掉链表中相应的元素值 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...
- 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 -- ...
- (easy)LeetCode 203.Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- Java [Leetcode 203]Remove Linked List Elements
题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...
随机推荐
- 利用red5搭建一个简单的流媒体直播系统
http://blog.sina.com.cn/s/blog_51396f890102exmz.html 一.red5安装.设置. 这个过程就不多说了,参见http://blog.csdn.net/l ...
- UVaLive 7269 Snake Carpet (找规律,模拟)
题意:给定一个数字n,表示有n条蛇,然后蛇的长度是 i ,如果 i 是奇数,那么它只能拐奇数个弯,如果是偶数只能拐偶数个,1, 2除外,然后把这 n 条蛇, 放到一个w*h的矩阵里,要求正好放满,让你 ...
- SQL大数据查询分页存储过程
最后一页分页一卡死,整个网站的性能都会非常明显的下降,不知道为啥,微软有这个BUG一直没处理好.希望SQL2012里不要有这个问题就好了. 参考代码如下: -- =================== ...
- 在SQL Server 2014下面使用的SQL2000的Northwind和Pubs示例数据库
在SQL Server 2014下面使用的SQL2000的Northwind和Pubs示例数据库 http://files.cnblogs.com/files/lxzhangying/SQl2000S ...
- CUDA从入门到精通
http://blog.csdn.net/augusdi/article/details/12833235 CUDA从入门到精通(零):写在前面 在老板的要求下.本博主从2012年上高性能计算课程開始 ...
- Codeforces Round #114 (Div. 1) A. Wizards and Trolleybuses 物理题
A. Wizards and Trolleybuses Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...
- C++ Brush
关键点 实现过程 void CCreateBrushView::OnDraw(CDC* pDC) { CCreateBrushDoc* pDoc = GetDocument(); AS ...
- Android动画的实现 上
在Android系统中也能经常见到动画,那么如何实现动画效果呢?本文就来为大家介绍动画的实现方式. Android中动画的实现分两种方式,一种方式是补间动画Tween Animation,就是说你定义 ...
- android应用程序监听SMS Intent广播
当设备接收到一条新的SMS消息时,就会广播一个包含了android.provider.Telephony.SMS_RECEIVED动作的Intent. 对于应用程序监听SMS Intent广播,首先需 ...
- boost.asio源码剖析(一) ---- 前 言
* 前言 源码之前,了无秘密. ——侯捷 Boost库是一个可移植.提供源代码的C++库,作 ...