203. Remove Linked List Elements
2016-03-21 20:45
465 查看
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
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
/** * 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 *p=head; if(head==NULL)return NULL; while(head) {//因为有(1,1)1 ;(1)1 这样的数据, if(head->val==val) head= head->next; else break; } while(p->next){//这里是考虑的下一个节点的值,不能包含第一个节点就是要删除的,这种情况 if(p->next->val==val) p->next=p->next->next; else p=p->next; } return head; } };
相关文章推荐
- JVM(2)——JVM类加载机制
- 数据持久化存储回顾
- ios三方插件
- QT学习之路————重写键盘事件
- Ubuntu14.04LTS下应用Caffe训练Cifar10错误案例分析与解决方案1
- something about Parameter Estimation (参数估计)
- Android Studio 快捷键
- 如何查看本地Git的html帮助文档
- socket编程(二)---- 简单的服务器端
- 优先队列
- 软件看门狗程序
- WebService 安卓客户端调用和服务器端搭建
- CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp
- 基于DDS的任意波形发生器
- Java+MySQL实现网络爬虫程序
- MySQL 入门 之 联接关键字
- 智能指针的理解
- CALayer Animation实践(一):让应用灵动起来!
- 初学统计学(一)
- ios管理插件