LeetCode题解——Remove Linked List Elements
2015-07-29 11:05
288 查看
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.
注意,若代码中ListNode* newlist = newhd->next;
最后return newlist,则结果将出现错误
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.
* struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* removeElements(ListNode* head, int val) { ListNode* newhd = new ListNode(0); newhd->next = head; ListNode* newlist = newhd; while(newhd!=NULL && newhd->next!=NULL){ if(newhd->next->val == val) { newhd->next = newhd->next->next; } else newhd = newhd->next; } return newlist->next; } };
注意,若代码中ListNode* newlist = newhd->next;
最后return newlist,则结果将出现错误
相关文章推荐
- poj 1625 AC自动机+动归+大数加法
- Linux 多线程调试(内存占用、死循环、CPU占用率高……)
- 对fork函数理解
- 【Java】取当前.class文件的编译位置
- MVVM
- DateUtil
- 消息机制和RPC的区别
- vim 操作技巧
- php使用gzip压缩传输js和css文件的方法
- 手机短信验证码的实现
- Android官方命令深入分析之bmgr
- Rational Team Concert 2
- 与Linux关机、重启相关的几个命令
- hdoj 4539 郑厂长系列故事——排兵布阵 【状压DP入门】
- Zend Framework1-Zend_Controller_Request请求对象的封装
- Android官方命令深入分析之bmgr
- 使用MJExtension中要注意的地方
- JavaScript中的cacheStorage使用详解
- redis+keepalived高可用
- subprocess shell=True的测试