Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

解题思路:

修改上题代码即可,JAVA实现如下:

public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode temp = head.next;
if (temp.val == head.val) {
while (temp.val == head.val) {
temp = temp.next;
if (temp == null) {
head.next = null;
return head;
}
}
head.next = temp;
}
temp = head.next;
ListNode last = head;
while (temp != null && temp.next != null) {
if (temp.val != temp.next.val) {
last.next = temp;
temp = temp.next;
last = last.next;
continue;
}
last.next = temp;
last=last.next;
while (temp.val == last.val) {
temp = temp.next;
if (temp == null) {
last.next = null;
return head;
}
}
}
last.next = temp;
return head;
}

Java for LeetCode 083 Remove Duplicates from Sorted List的更多相关文章

  1. Java for LeetCode 080 Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...

  2. Java for LeetCode 026 Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  3. Java for LeetCode 082 Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  4. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  5. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  6. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  7. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  8. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  9. [LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbe ...

随机推荐

  1. novell.directory.ldap获取邮箱活动目录

    在windows系统上可以使用下列方法来查找所有的员工邮箱和员工组: StringDictionary ReturnArray = new StringDictionary(); Dictionary ...

  2. Form元素示例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. EasyMvc入门教程-高级控件说明(15)方位布局控件

    现在很多管理后台都流行全屏切割的布局,大体结构如下图所示: 大家注意到没,整个布局是五个部分组成:“东西南北中”,EasyMvc对应的实现的代码为: @(Html.Q().Layout().TextC ...

  4. dubbo应用程序的单元测试环境搭建(springtest,powermock,mockito)

    转:http://blog.csdn.net/yys79/article/details/66472797 最近,项目中频繁用到dubbo,而且java工程用引用了几十个关联系统的服务(如用户认证,基 ...

  5. UVA LIVE-4642 - Malfatti Circles

    给出三角形三个顶点,求出三个互切的圆的半径 尽管大白鼠说能够推出公式,但是这个公式仅仅怕没那么easy推--我左看右看上看下看也推不出. 应该是要做辅助线什么的,那也-- 因为非常easy就推出了关于 ...

  6. 基于Repository模式设计项目架构—你可以参考的项目架构设计

    关于Repository模式,直接百度查就可以了,其来源是<企业应用架构模式>.我们新建一个Infrastructure文件夹,这里就是基础设施部分,EF Core的上下文类以及Repos ...

  7. 工作总结 a标签 <a href="/meetingtheme">Back to List</a> 返回上一级 指向 控制器 默认Index @Html.ActionLink("Edit59", "Edit", new { id = item.ID }) 默认当前控制器

    @Html.ActionLink("Back to List", "Index")  ----  <a href="/doctorinfo&qu ...

  8. java 内存与内存溢出

    学习自:http://www.codeceo.com/article/jvm-memory-overflow.html 讲的很清楚

  9. js高度line-height及宽度text-align:center居中插件

    1.高度居中---在高度设为100%,无法直接使用line-height:100%;会不起效果 这是用于应对height:100%的插件 /** * 高度居中函数,用于应对高度设为100%时的居中 * ...

  10. matlab2016b-linux版本在ubutu16.04x64上面不能打开摄像头的处理方法

    this  can  not  work.    need  find  other way.   ================================================== ...