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 --> ...
 
随机推荐
- Apache Spark Mesos
			
Mesos是一个资源管理框架,提供类似于YARN的功能. 用户可以在其中插件式地运行Spark. MapReduce. Tez等计算框架的任务. Mesos会对资源和任务进行隔离,并实现高效的资源任务 ...
 - html学习笔记之position
			
今天主要一直看试验position的各种属性,现在记录下来以此备忘. position有四种常有属性,分别是static,fixed.absolute,relative fixed就是相对于窗口的位置 ...
 - 四轴飞行diy全套入门教程(从最基础的开始)
			
转载:http://www.cnmox.com/thread-12460-1-1.html首先声明本人也是菜鸟,此教程就是从一个菜鸟的角度来讲解,现在论坛上的帖子都突然冒很多名词出来,又不成体系,我自 ...
 - Session,Cookie 和local storage的区别
			
以前从没有听说过local storage, 在网上查了一些资料,得到如下结论 从存储位置看,分为服务器端存储和客户端存储两种 服务器端: session 浏览器端: cookie, localSto ...
 - 转载C# 对象转Json序列化
			
转载原地址: http://www.cnblogs.com/plokmju/p/ObjectByJson.html JSON Json(JavaScript Object Notation) 是一种 ...
 - word2007 每页显示表头
			
word2007 每页显示表头 在Word 2007文档中,如果一张表格需要在多页中跨页显示,则设置标题行重复显示很有必要,因为这样会在每一页都明确显示表格中的每一列所代表的内容.在Word 2007 ...
 - hibernate之saveorupdate()、save()、update()都有什么区别
			
saveorupdate()如果传入的对象在数据库中有就做update操作,如果没有就做save操作. save()在数据库中生成一条记录,如果数据库中有,会报错说有重复的记录. update()就是 ...
 - C++ 动态创建对象
			
转自:http://www.cnblogs.com/jisi5789/p/3190353.html 回顾前面的文章,实现了一个简单工厂模式来创建不同类对象,但由于c++没有类似new "Ci ...
 - hdu 4494 Teamwork 最小费用最大流
			
Teamwork Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4494 ...
 - MyBatis之八:需要说明的几个java api的生命周期以及封装
			
学习mybatis不得不了解SqlSessionFactoryBuilder.SqlSessionFactory.SqlSession.这里主要是讲解它们的生命周期以及一般最佳实践. 一般来说对象的生 ...