leetcode-Remove Linked List Elements
2015-11-07 22:40
190 查看
Difficulty: Easy
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
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 *dump=new ListNode(-1); dump->next=head; ListNode *p=dump; ListNode *q=head; while(q){ bool is_dele=false; if(q->val==val){ is_dele=true; while(q->next&&q->val==q->next->val) q=q->next; } if(is_dele) p->next=q->next; else p=p->next; q=q->next; } return dump->next; } };
相关文章推荐
- 2,XCode的快捷键
- 【Oracle】无宕机迁移更换asm磁盘
- JavaScript高级程序设计之DOM2和DOM3之样式之访问元素的样式第12.2.1讲
- 剑指offer之65 滑动窗口的最大值问题
- Andrid的网络开发技术
- 3.0MongoDB对一个collection的常用操作
- 汉字转拼音
- D - 粉碎叛乱F - 其他起义
- gradle eclipse web模板项目构建
- 2817:木棒 【递归+剪枝】
- exej4打包jar图文教程
- Java String之intern()方法深入分析
- 3.0创建mongoDB客户端链接
- DNS弹窗广告遭遇
- 关于html中CSS的小结
- Swift开发基础篇—基本语法
- codeforces 592D 树链剖分
- hdoj 5124 lines【】
- java静态代码块、初始化块和构造方法的执行顺序
- JLINK驱动刷新之WIN7