203. Remove Linked List Elements (List)
2015-12-17 09:06
357 查看
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
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* newHead = new ListNode(0); newHead->next = head; ListNode* pre = newHead; ListNode* cur = head; while(cur){ if(cur->val == val){ pre->next = cur->next; } else{ pre = pre->next; } cur = cur->next; } return newHead->next; } };
相关文章推荐
- eclipse一般文本文件怎么用html打开
- java集合——集合与数组间的转换+算法
- android 中使用Zxing实现二维码的解码和二维码的生成
- 写文件
- Javascript中的函数(Function)与对象(Object)的关系
- SpringAOP模块初始化过程
- nginx日志分割脚本
- linux上安装mysql
- 怎样把本地的jar包引入到maven工程里面
- 查看字段长度
- 代码实现SQL Server动态行转列,不用存储过程
- 三层的搭建基础
- K60_CAN
- 关于重复UI属性的设计
- 名人名言
- 互联网公司的技术博客汇总-阿里腾讯百度等
- Java date format 时间格式化
- 合并多行查询数据到一行:使用自连接、FOR XML PATH('')、STUFF或REPLACE函数
- [编]IoT The Internet of Things (IoT) 物联网
- 解析html