`

LeetCode 82 - Remove Duplicates from Sorted List II

 
阅读更多

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

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

 

public ListNode deleteDuplicates(ListNode head) {
    ListNode dummy = new ListNode(0);
    ListNode node = dummy;
    while(head != null) {
        ListNode p = head;
        while(head.next != null && head.val == head.next.val) {
            head = head.next;
        }
        if(p == head) {
            node = node.next = p;
        }
        head = head.next;
    }
    node.next = null;
    return dummy.next;
}

 

分享到:
评论

相关推荐

    leetcode浇花-LCSolutions:我的力扣解决方案

    leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates...

    leetcode答案-leetcode-java:leetcode的Java代码

    leetcode 答案leetcode-java leetcode.com 的 Java 答案 ================索引================ com.leetcode.array Search a 2D Matrix Spiral Matrix com.leetcode.list Linked List Cycle Linked List Cycle II ...

    leetcode中325题python-leetcode:leetcode

    remove-duplicates-from-sorted-list ii 83 删除排序链表中的重复元素 remove-duplicates-from-sorted-list 86 分隔链表 partition-list 92 反转链表 II reverse-linked-list-ii(Reverse a Sub-list) 141 环形链表...

    LeetCode最全代码

    26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates...

    javalruleetcode-leetcode-java:力码笔记

    26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock III 141....

    LeetCode:关于LeetCode.com和ACM的一些算法问题

    Remove Duplicates from Sorted List II题目: | 源码:标签:单向链表难度:中等 / Medium146. LRU Cache题目: | 源码:标签:哈希表,双向链表难度:中等 / Medium212. Word-Search-II题目: | 英文站源码:./...

    leetcode分发糖果-ForDaFactory:使用C++的个人leetcode解决方案

    83-删除排序链表中的重复元素:remove-duplicates-from-sorted-list 92-反转链表II:reverse-linked-listt-ii 141-环形链表:linked-list-cycle 142-环形链表:linked-list-cycle-ii 160-相交链表:intersection-of-two-...

    leetcode-js:算法和数据结构是一个程序员的灵魂,LeetCode JavaScript TypeScript 题解

    83.删除排序链表中的重复元素 (Remove Duplicates from Sorted List) 88.合并两个有序数组 (Merge Sorted Array) 100.相同的树 (Same Tree) 104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 ...

    leetcode2sumc-LeetCode:LeetCode的一些题目

    leetcode 2 sum c LeetCode 帮助文档 帮助文档存放在Help文件夹下。 文件名 文件描述 链接 complexitypython.txt Python的一些常规操作的复杂度统计 题目清单 Array(数组) ID Difficulty Title Java Python 1 Easy ...

    leetcode写题闪退-LeetCode:leetcodeOJ

    leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...

    leetcode2sumc--Offer:-提供

    leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类文章,欢迎您的关注! 帮助文档 帮助文档...

    lrucacheleetcode-LeetCode:LeetCode刷题

    II(Remove Duplicates from Sorted List II) 2018.9.27 重建二叉树(Rebuild Binary Tree) 2018.9.28 把字符串转换成整数(Convert a string to an integer) 2018.10.8 树的子结构(Substructure of the tree) ...

    leetcode2sumc-ack-CherishLeetCode:ack-CherishLeetCode

    leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类文章,欢迎您的关注! 帮助文档 帮助文档...

    cpp-算法精粹

    Remove Duplicates from Sorted List II Rotate List Remove Nth Node From End of List Swap Nodes in Pairs Reverse Nodes in k-Group Copy List with Random Pointer Linked List Cycle Linked List Cycle II ...

    leetcode-liwang:leetcode学习笔记

    O(m+n) time, O(m+n) sapce.*0026 Remove Duplicates from Sorted Array使用双指针,一个快指针,一个慢指针。开始时,两个指针都指向首元素。当两指针元素值相同时,快指针+1;当两指针元素不同时,慢

    leetcodepython001-LeetCode:力码

    leetcode python 001 LeetCode 建立两个个主要资料夹(题目收集、学习内容)+一个玩整的README整理 题目 主要以Python记录于VScode上 先记录头150题 学习 以JupyterNotebook为主 纪录各种资料结构、演算法等 ...

    删除排序链表中的重复元素

    链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/ 思路 由于是排序链表,所以只需判断当前节点的元素与下一个节点的元素是否相同,如果相同则将当前节点的指针指向大下个节点,如果不同...

    :猴子:LeetCode,剑指提供刷题笔记(C / c++, Python3实现)

    LeetCode 原创文章每周最少两篇,后续最新文章会在首发,视频首发,大家可以加我进交流群,技术交流或提意见都可以,欢迎Star! 帮助文档 帮助文档存放在Help文件夹下。...Remove Duplicates from Sorted Lis

    javalruleetcode-JavaInterviewChecklist:要检查的Java事项

    leetcode 面试清单 Java 比较器与比较器 哈希 静止的 例外 线 泛型 算法 链表 Linked List Cycle Remove Nth Node From End of List Merge Two Sorted Lists 两个链表的交集 Remove Duplicates from Sorted List ...

    Dir-For-LeetCode

    问题 完全的 017_Letter_Combinations_of_a_Phone_... 026_Remove_Duplicates_from_Sorted_Array 027_Remove_Element 028_Implement_strStr() 029_Divide_Two_Integers 030_Substring_with_Concatenation_of

Global site tag (gtag.js) - Google Analytics