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. IOS 暂停和恢复CALayer上的动画(转)

    coreAnimation的动画是存在于CALayer上面的,有些时候需要突然暂停某个组件的动画效果,同时保留当前动画的状态, 如果是用removeAnimation会显得很突兀,不够平滑,所以可以利 ...

  2. td中内容自动换行

    使用<table> 进行页面开发,会遇到字符串很长将table撑开变形的问题,在网上搜了一些,终于找到 一个比较好用的解决方法.贴出来,方便以后使用.在table标签中加入如下的style ...

  3. (转)python request用法

    强烈推荐!requests官方文档已有了中文版,请见http://cn.python-requests.org/zh_CN/latest/ requests是python的一个HTTP客户端库,跟ur ...

  4. 计算机图形学OpenGL中的glLoadIdentity、glTranslatef、glRotatef原理,用法 .(转)

    单位矩阵 对角线上都是1,其余元素皆为0的矩阵. 在矩阵的乘法中,有一种矩阵起着特殊的作用,如同数的乘法中的1,我们称这种矩阵为单位矩阵. 它是个方阵,除左上角到右下角的对角线(称为主对角线)上的元素 ...

  5. nginx 按天生成日志

    nginx日志access.log error.log按天生成存储,定时删除日志 logrotate https://linux.cn/article-4126-1.html http://blog. ...

  6. bat文件转换为exe文件

    批处理文件转换为exe文件(简单的处理文件),点击下载 使用超简单的了,不多说.

  7. metaq入门部署到实战

    初识metaq zookeeper部署,这里单机zk为例. wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.5/zookeep ...

  8. Oracle中Hint深入理解(原创)

    http://czmmiao.iteye.com/blog/1478465 Hint概述  基于代价的优化器是很聪明的,在绝大多数情况下它会选择正确的优化器,减轻了DBA的负担.但有时它也聪明反被聪明 ...

  9. HPE IT 的DevOps 实践分享

    原文地址:http://www.codes51.com/article/detail_3124576.html 本篇文章来自于HPE和msup共同举办的技术开放日HPE测试技术总监肖俊的分享,由壹佰案 ...

  10. VueJS组件之全局组件与局部组件

    全局组件 所有实例都能用全局组件. HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...