Java [Leetcode 203]Remove Linked List Elements
2016-02-06 15:34
435 查看
题目描述:
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
解题思路:
链表操作。
代码如下:
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
解题思路:
链表操作。
代码如下:
/** * 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) { ListNode res = new ListNode(0); res.next = head; ListNode point = head, pre = res; while(point != null) { if(point.val == val) { if(point.next != null) { point.val = point.next.val; point.next = point.next.next; } else { point = null; pre.next = null; } } else { point = point.next; pre = pre.next; } } return res.next; } }
相关文章推荐
- 【Java】javaIO之带缓冲流的字符流文件读写
- 【Java】javaIO之带缓冲流的字节流文件读写
- 【Java】javaIO之用随机流读写文件
- 【Java】javaIO之用FileReader和writer读写文本文件
- Spring MVC遭遇checkbox的问题解决方式
- ZooKeeper客户端支持的语言
- Java 将HTML页面转换成DOM
- 各种排序算法知识总结
- 常用eclipse快捷键
- java学习心得——添加列表查询功能
- Maven搭建SpringMVC项目流程[详细]
- Java [Leetcode 290]Word Pattern
- 修改Eclipse与MyEclipse的代码提示功能+取消空格上屏
- spring struts2 mybatis框架学习总结(mvc三层架构)
- springmvc(十六)springmvc注解开发-springmvc参数绑定-map绑定
- spring 4.x + struts 2.x + mybatis 3.x - getting started
- springmvc(十五)springmvc注解开发-springmvc参数绑定-list绑定
- Eclipse错误提示:The project was not built since its build path is incomplete. Cannot find the class file
- Java [Leetcode 229]Bulls and Cows
- JavaWeb_Servlet讲解(一)