Remove Linked List Elements
2016-06-10 13:45
393 查看
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.
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.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode removeElements(ListNode head, int val) { if(head == null) return head; ListNode dump = new ListNode(0); dump.next = head; ListNode cur = dump; while(cur!=null && cur.next != null ){ if(cur.next.val == val){ cur.next = cur.next.next; } else { cur = cur.next; } } return dump.next; } }
相关文章推荐
- Java知识点梳理系列(序)
- Gdal 2.1.0 示例工具的二次开发
- 农村土地确权之调查公示 —— 土地确权二轮公示及后续工作注意事项
- Android四大组件之Service
- SpringMVC之http的状态码(五)
- NPTL vs PThread
- javaBean规范有哪些内容?
- [企业管理]员工生育保障问题的讨论
- python+selenium环境搭建
- 浅入ARP
- 抽象类和接口的区别
- Oracle 11g R2+RAC+ASM+OracleLinux6.4安装详解(图)
- 1 thinkphp 如何访问控制层
- 算法与数据结构之简单排序
- jstl标签不起作用不管用,jstl标签直接输出表达式
- Failed to get the adb version: "/android-sdk/tools/adb"权限不够
- Android自定义View
- 如何将latex模板中的照片框去掉?
- Win 7 64位 单机Spark安装
- 第三方轮子处理内容的汇总