[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
问题:给定列表表头,删除列表中值为 val 的元素。
比较方便的方法是,每次比较 p->next->val 与 val ,当相等时跳过 p->next 即可: p->next = p->next->next;
ListNode* removeElements(ListNode* head, int val) {
ListNode* p = new ListNode();
p->next = head;
ListNode* pMake = p;
while(p->next != NULL){
if (p->next->val == val){
p->next = p->next->next;
}else{
p = p->next;
}
}
head = pMake->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题要求 ...
- 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 -- ...
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 递归 日期 题目地址:https://lee ...
- 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 (移除链表中的项)
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 --& ...
- (easy)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 链表
去掉链表中相应的元素值 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...
随机推荐
- android Json解析详解
JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语 言的支持),从而可以在不同平台间进行数 ...
- STL之Pairs
什么是Pair 关于类Pair的介绍,下面是引自<C++ Standard Library>的一段话: The class pair is provided to treat two va ...
- Windows与自定义USB HID设备通信说明.
1 . 所使用的典型 Windows API CreateFile ReadFile WriteFile 以下函数是 DDK 的内容: HidD_SetFeature HidD_GetFeatur ...
- Hadoop安装测试简单记录
安装的节点如下:1个namenode.1个hiveserver.3个dataNode192.168.1.139 namenode1192.168.1.146 hiveserver 192.16 ...
- api接口
目录(?)[-] 接口特点汇总 PHP Token令牌 先说第一个tokenapi_token 服务端接口校验PHP实现流程如下 再说第二个tokenuser_token 接口用例如下 接口特点汇总: ...
- Linux 最大进程数
前言 使用环境:centos 7系统 一.查看用户打开的最大进程数 ulimit -a max user processes (-u) #系统限制某用户下最多可以运行多少进程 ...
- jdbc02
分层实现新闻管理系统 1.创建新闻信息实体类,jdbc配置文件以及工具类 public class News { // 新闻信息的实体类 private Integer id; //编号 privat ...
- TextView过长显示省略号, TextView文字中间加横线
1.TextView显示的内容过长时自动显示省略号: 省略号的位置:android:ellipsize="end" 省略号在结尾android:ellipsize=" ...
- Arcgis android - Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE
报错: Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE Please check logcat output for more deta ...
- Nagios设置只监控不报警
设置全部监控项都开启邮件报警: vim /usr/local/nagios/etc/nagios.cfg 设置 enable_notifications=1 1为开启,0为关闭 如个别监 ...