`
frank-liu
  • 浏览: 1666075 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

leetcode: Remove Duplicates from Sorted List

 
阅读更多

问题描述:

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.

原问题链接:https://leetcode.com/problems/remove-duplicates-from-sorted-list/

 

问题分析

  这个问题和之前讨论过的移除重复元素的问题很接近。它们有一个细微的差别就是在前一个问题中,对于有任何重复的元素,它们将不能包含在结果链表中,而这个是要求重复的元素有一个要包含在结果连表中。虽然有这么一个细微的差别,但是它们的基本解决思路还是很近似的。

  我们通过一个临时的ListNode来保存指向head前一个元素,并将它设置为pre。然后将cur = head。这样每次根据cur.next != null的循环去判断cur.val == cur.next.val。如果不相等说明cur的下一个元素就要和当前元素不同了,那么需要将prev.next = cur。在退出循环后要将最后一个元素也链接到链表中来。

  详细的代码实现如下:

 

/**
 * 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 res = new ListNode(0);
        res.next = head;
        ListNode pre = res, cur = head;
        while(cur.next != null) {
            if(cur.val != cur.next.val) {
                pre.next = cur;
                pre = cur;
            }
            cur = cur.next;
        }
        pre.next = cur;
        return res.next;
    }
}

  

 

分享到:
评论

相关推荐

    leetcode2sumc-LeetCode:LeetCode的一些题目

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

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

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

    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) ...

    leetcodepython001-LeetCode:力码

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

    leetcode写题闪退-LeetCode:leetcodeOJ

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

    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答案-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-js:算法和数据结构是一个程序员的灵魂,LeetCode JavaScript TypeScript 题解

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

    leetcode-liwang:leetcode学习笔记

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

    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...

    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分发糖果-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最全代码

    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...

    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 ...

    leetcode2sumc--Offer:-提供

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

    leetcode2sumc-ack-CherishLeetCode:ack-CherishLeetCode

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

    :猴子: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

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

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

Global site tag (gtag.js) - Google Analytics