[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 ...
随机推荐
- CComPtr用法
COM接口指针很危险,因为使用过程中需要每一个使用者都要严格并且正确的AddRef和Release,一旦出现问题,就会造成对象不能被正常释放,或者对象被重复删除,造成程序崩溃.所以使用COM接口,必须 ...
- yii cgridview 对生成的数据进行分页
这个其实最简单 在对应的model里找Search方法 找到后,参见如下代码 public function search() { // @todo Please modify the followi ...
- ls Common Command-Line Options
ls Common Command-Line Options Command Use: ls -l Shows a long listing, which includes informat ...
- MM32Flash读写操作(兼容STM32)
MM32Flash读写操作(兼容STM32) Flash基础描述 思维导图 编程实现读写操作 主函数结构 #include "delay.h" #include "sys ...
- 安装Microsoft oneDrive(原skyDrive)
oneDrive下载地址:https://onedrive.live.com/about/zh-cn/download/ 安装时报错:Error 0x80040ca0 解决方案:关闭安装程序,按下面的 ...
- UFLDL课程学习(二)
章节地址:http://ufldl.stanford.edu/tutorial/supervised/LogisticRegression/ 章节名称:逻辑回归 (Logisitic Regressi ...
- 'Invalid update: invalid number of rows in section xx. The number of rows contained in an existing section after the update (xxx)...
'Invalid update: invalid number of rows in section 5. The number of rows contained in an existing s ...
- C++拾遗(七)函数相关(2)
内联函数 内联函数与常规函数的区别在于: 1.常规函数:在执行调用指令时,先存储该指令的内存地址,将函数参数复制到堆栈,然后跳转到被调用函数起点的内存单元,执行函数,将返回值放 入寄存器,最后跳回到一 ...
- /etc/host 配置主机名字
每个机子中的hosts文件都应有下面域IP对应的文件
- CentOS 7 之Cisco Anyconnect Secure Mobility Client
公司使用的是Cisco VPN, 于是准备使用一下.先登录公司的vpn页面,意料之中的失败,所以下载了vpnsetup.sh这个来手动安装. 手动是要用root的,不过由于我是个人学习使用机器,一直用 ...