203. Remove Linked List Elements
2016-07-08 23:23
309 查看
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.
Subscribe to see which companies asked this question
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.
Subscribe to see which companies asked this question
/** * 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* temp=head; ListNode* pre=NULL; while(head) { if(head->val==val) { if(pre!=NULL) { pre->next=head->next; } else { temp=head->next; } } else { pre=head; } head=head->next; } return temp; } };
相关文章推荐
- 嵌入式入门知识
- Why would you learn C++ in 2016?(2016年,为什么我们要学习C++)
- Why would you learn C++ in 2016?(2016年,为什么我们要学习C++)
- Masory框架
- Two Sum
- LeetCode 072 Edit Distance
- 模仿UIApplication单例实现方式
- Codefroces 675 B. Restoring Painting
- poj 1182 【经典并查集】 和【不一样的解法】
- HDU2044 一只小蜜蜂...
- iOS_解析XML(很实用的两框架:KissXML、XmlReader)
- Netty中的连接超时问题
- 错误1402 安装程序无法打开注册表项
- 字符串的操作
- RxJava 并发之线程调度
- Linux之Tomcat开机自动启动
- 学徒浅析Android开发:杂谈——仿QQ列表左右滑动效果
- TextBox控件
- 数据结构高分笔记1327||HNCU1327:算法2-13~2-16:静态链表
- Android init进程一些容易忽视的技术细节