LeetCode 83 -Remove Duplicates from Sorted List ( JAVA )
2016-04-15 16:57
603 查看
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null) return head;
ListNode ls = head;
while(head.next != null){
if(head.val == head.next.val){
head.next = head.next.next;
}else{
head = head.next;
}
}
return ls;
}
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/7fc6ce868ff680c8dd9f456522067625)
总结:主要就是一个循环比较值,没什么难度,就是要注意不要有空指针的情况出现。
For example,
Given
1->1->2, return
1->2.
Given
1->1->2->3->3, return
1->2->3.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null) return head;
ListNode ls = head;
while(head.next != null){
if(head.val == head.next.val){
head.next = head.next.next;
}else{
head = head.next;
}
}
return ls;
}
}
总结:主要就是一个循环比较值,没什么难度,就是要注意不要有空指针的情况出现。
相关文章推荐
- java装箱拆箱理解
- Hierbate 3.3+Spring 3.1 +SpringMVC+Myeclipse 10集成框架搭建
- Java HashSet概述
- Eclipse 文件管理介绍
- Java IO3:字符流
- 每天一道算法题(四) (动态规划算法)01背包问题Java实现
- Eclipse开发环境下,部署和配置svn的整个过程
- spring 注解 出现'sessionFactory' or 'hibernateTemplate' is required异常
- Java 正则中的(.*?)vs(.*)
- Java泛型的理解
- struts2前台传递List、Set、Map集合数据到后台
- java线程同步中使用自增操作时的注意
- java中序列化和反序列化
- JAVA第五次作业
- Java并发编程:Condition实现线程间协作
- java动态拼接datagrid的columns
- Spring MVC国际化的总结
- eclipse无法启动的各种解决方法
- java第四次作业(补)
- Android开发在Eclipse提示信息This element neither has attached source nor attached Javadoc