LEETCODE 203
2016-03-23 21:50
323 查看
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
主要是头节点的处理。可以用判断头节点的方式;我用的生成一个新节点,令其指向头节点。
/**
* 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 tempHead(val+1);
ListNode* tempPos = &tempHead;
tempHead.next = head;
while(tempPos->next)
{
if(val == tempPos->next->val)
tempPos->next = tempPos->next->next;
else
tempPos = tempPos->next;
}
return tempHead.next;
}
};
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 tempHead(val+1);
ListNode* tempPos = &tempHead;
tempHead.next = head;
while(tempPos->next)
{
if(val == tempPos->next->val)
tempPos->next = tempPos->next->next;
else
tempPos = tempPos->next;
}
return tempHead.next;
}
};
相关文章推荐
- bzoj3638&&【GDOI2016模拟3.20】diyiti
- 【AngularJS】——核心特性之路由
- ORA-00918 未明确定义列,with t as 后面只能跟select
- PHP Include 文件
- 安装谷歌浏览器出现错误代码 0x80004002 解决方法
- CF_3A_ShortestPathOfTheKing
- dreamvc框架(一)ioc容器的集成
- 我的java学习之事件监听篇01
- 第三次作业3
- Django: 之用户注册、缓存和静态网页
- Impala 2、Impala Shell 和 Impala SQL
- Wordcount
- vr的延迟和渲染效率优化与Nvidia VRWorks
- windows 7 命令行下设置全局环境变量
- LEETCODE 205
- html入门
- HDU3657 Game(最小割)
- 不允许用大于号小于号,比较任意两个数字大小
- PSP记录个人项目耗时情况
- iOS instancetype与id的比较