(LinkedList) Remove Linked List Elements
2016-04-06 15:08
148 查看
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. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode removeElements(ListNode head, int val) { while(head!=null && head.val==val) head=head.next; if(head==null) return head; ListNode cur=head; while(cur!=null && cur.next!=null){ if(cur.next.val==val){ cur.next=cur.next.next; } else cur=cur.next; } return head; } }
相关文章推荐
- CodeIgniter集成Smarty的最佳方式
- PVS更新Vdisk大型环境中提升为测试版
- 一张图解释page、client、offset、screen区别
- 专题二 · 1009
- JSON 使用 转换为对象
- 约瑟夫问题(密码相同和密码不同两种情况)
- ios-复制字符串到剪贴板
- HDOJ 1236 排名(练耐心题)
- Windows Server 2008 显示桌面图标
- HDOJ 1236 排名(练耐心题)
- HTML 表格 做个人简历
- linux下生成rsa私钥公钥
- Restful的定义
- linux yum 安装mysql 5.6
- Dreamweaver中实时视图乱码
- easyui 动态修改窗口title
- Collection 中ArrayList、LinkedList、Vector、Set的比较与运用
- js执行顺序问题
- DispSync
- python处理中文文件名