【一天一道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 = ...
随机推荐
- bzoj 4518: [Sdoi2016]征途
Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜 ...
- bzoj2500幸福的道路 树形dp+单调队列
2500: 幸福的道路 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 434 Solved: 170[Submit][Status][Discuss ...
- 一起来Fit TDMA over WiFi(2)
3 收发流程分析与改进 收发流程分析涉及到具体代码,属于SDK驱动内容,不能完全公开,仅供参考,本系列文档中涉及到具体功能或代码时,请在自己的驱动代码中查找. QCA驱动从9.5开始,将原来的htc的 ...
- Linux学习之CentOS(十八)-----恢复Ext3下被删除的文件与 使用grep恢复被删文件内容(转)
前言 下面是这个教程将教你如何在Ext3的文件系统中恢复被rm掉的文件. 删除文件 假设我们有一个文件名叫 'test.txt' $ls -il test.txt 15 -rw-rw-r– 2 roo ...
- linux常用命令随记
常用指令 ls 显示文件或目录 -l 列出文件详细信息l(list) -a 列出当前目录下所有文件及目录,包括隐藏的a(all) mkdir 创建目录 -p 创建目录,若无父目录,则创建p(paren ...
- c#默认类的修饰符。
类的默认修饰符是internal 当其修饰符为internal时,其他模块不能访问该类, 所以class类的修饰符一般为public
- jQuery 效果 – 淡入淡出
在在jQuery中可以通过四个方法来实现元素的淡入淡出,这四个方法分别是:fadeIn().fadeOut().fadeToggle() 以及 fadeTo(),本文通过实例来为你讲解如何在jQuer ...
- Xcode在playground的quick look框中显示对象自定义视图
对于一般对象,playground中默认的quick look显示已经够用,比如简单的字符串,Int,或简单的自定义Class等等. 不过对于有些情况,我们需要自定义对象在playground中的显示 ...
- python模块:时间处理模块
http://blog.csdn.net/pipisorry/article/details/53067168 常用python自带时间处理模块 python自带的时间处理模块参考[操作系统服务:ti ...
- 23 服务的创建Demo1
结构 MainActivity.java package com.qf.day23_service_demo1; import android.app.Activity; import android ...