[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 ...
随机推荐
- Chapter 1. Introduction gradle介绍
We would like to introduce Gradle to you, a build system that we think is a quantum leap for build ...
- NYOJ-745蚂蚁的难题(二)
这道题和求字段和的要求就差一点,就是那个是一条链, 这个是个环,关于这么环,刚开始按照链那种方式推倒状态转移方程,但是没有写出来,后来看题解,才看到原来还是转化为普通的单链来做,好多题都是由不会的转化 ...
- 设计模式之—简单工厂模式<Simple Factory Pattern >
简单工厂模式结构图: 简单工厂模式以简单的加减乘除运算为例: 运算符类(Operation): namespace CalcTest.Simple_Factory_patterns { class O ...
- GIt/Github常用命令
1)git init:初始化本地仓库 2)创建文件:touch read.txt 3)当操作本地的文件时,使用常用的命令,如(mv,ls..)就可以操作,当操作暂存区的文件时需要在命令前家git,并且 ...
- Zend Framework
参考:http://www.php100.com/manual/ZendFramework/index.html 1.1. 概述 Zend Framework (ZF) 是一个开放源代码的 PHP5 ...
- 关于JavaScript对象的键和值
一个JavaScript对象由键和值组成. 当一个给定键的值被设置为一个字符串.布尔值.数字.数组或对象时,我们把这个键称为属性. 当把键设置为函数时,我们把它叫做方法.
- (二)Angularjs - 入门(2)
AngularJS AJAX AngularJS提供了$http控制,可以用来获取服务器端的数据.服务器通过一个数据库的读取操作来获取需要的数据.注意AngularJS需要JSON格式的数据.一旦数据 ...
- Caused by: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "mil_id")
今天在使用mybatis处理数据库的时候,突然抛出了上述异常,让我感到很惊讶,因为在处理save的时候,在Mybatis的配置文件中,我根本就没有使用到ognl表达式,系统怎么会抛出上述异常.而且之前 ...
- mysql实现增量备份
有点要注意 如果你误删了表 想通过这个恢复 必须恢复日志里面有创建表的日志 不然的话是无法回复的 就是必须是从你开始创建表的时候就已经记录日志了 恢复到哪个位置 就按照哪个位置来计算 mysql ...
- ASP.NET页面传值与跳转
asp.net页面传值的五种方法:QueryString,Session,Application,Request.Cookies,Server.Transfer 其中Cookie和Server.Tra ...