【一天一道Leetcode】#203.Remove Linked List Elements
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
我的个人博客已创建,欢迎大家持续关注!
一天一道leetcode系列依旧在csdn上继续更新,除此系列以外的文章均迁移至我的个人博客
另外,本系列文章已整理并上传至gitbook,网址:点我进
欢迎转载,转载请注明出处!
(一)题目
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.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* pre = NULL;
ListNode* p = head;
while(p){
if(p->val==val){//与给定值相等
while(p&&p->val==val) p=p->next;//连续相等的情况
if(pre==NULL) head = p;//如果需要删除头节点,则需要更新头节点
else pre->next = p;
}
else{
pre = p;
p=p->next;
}
}
return head;
}
};
【一天一道Leetcode】#203.Remove Linked List Elements的更多相关文章
- 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 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 --> ...
- [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 移除链表元素 C++/Java
Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...
- Leetcode 203 Remove Linked List Elements 链表
去掉链表中相应的元素值 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...
- [leetcode]203. Remove Linked List Elements链表中删除节点
这道题很基础也很重要 重点就是设置超前节点 public ListNode removeElements(ListNode head, int val) { //超前节点 ListNode pre = ...
随机推荐
- SparkSQL——用之惜之
SparkSql作为Spark的结构化数据处理模块,提供了非常强大的API,让分析人员用一次,就会为之倾倒,为之着迷,为之至死不渝.在内部,SparkSQL使用额外结构信息来执行额外的优化.在外部,可 ...
- c语言第三次作业。
---恢复内容开始--- (一)改错题 计算f(x)的值:输入实数x,计算并输出下列分段函数f(x)的值,输出时保留1位小数. 源代码 : 第一次编译: 错误原因:if 后面有分号 改正方法:去掉分号 ...
- 【TensorFlow 官网 可以直接访问】让中国开发者更容易地使用TensorFlow打造人工智能应用
人工智能的神奇之处,在于它能被应用在医疗保健.交通运输和环境保护等方方面面,为复杂的社会问题探寻解决方案.如今,在人工智能的协助下,人们得以探索全新的研究领域,开发创新的产品,让数以百万计的用户从中获 ...
- img图片占不满整个div
解决方法: img标签自带有3px的空隙,有很多解决方法第一种:设置img{font-size:0}第二种:设置img{display:block}第三种:设置img{vertical-align:t ...
- 基于无域故障转移群集 配置高可用SQLServer 2016数据库
基于上次的文章搭建的环境,可以在这里:http://www.cnblogs.com/DragonStart/p/8275182.html看到上次的文章. 演示环境 1. 配置一览 Key Value ...
- jdk和tomcat配置
1.一次成功的JAVA环境变量配置,必须要配置一下三个系统变量:JAVA_HOME(变量值为JDK的路径),PATH(变量值:%JAVA_HOME%\bin;),CLASS_PATH(变量值为JDK中 ...
- xshell连接centos与ubuntu
操作系统:Windows 7 应用软件:Ware Workstation &Xshell 5 Linux:CentOS 7 Minimal &Ubuntu Server 16 ==== ...
- PHP 实例 AJAX 投票
AJAX 投票 在下面的实例中,我们将演示一个投票程序,通过它,投票结果在网页不进行刷新的情况下被显示. Do you like PHP and AJAX so far? Yes: No: 实例解释 ...
- C++用LuaIntf调用Lua代码示例
C++用LuaIntf调用Lua代码示例 (金庆的专栏 2016.12) void LuaTest::OnResponse(uint32_t uLuaRpcId, const std::string& ...
- java.io.FileNotFoundException: D:\Program%20Files\Apache%20Software%20Foundation\Tomcat%205.0\webapp
慢慢把以前遇到过的问题一点点发出来,以前做的笔记比较杂: java.io.FileNotFoundException: D:\Program%20Files\Apache%20Software%20F ...