LeetCode83. Remove Duplicates from Sorted List

这篇具有很好参考价值的文章主要介绍了LeetCode83. Remove Duplicates from Sorted List。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、题目

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

Example 1:

Input: head = [1,1,2]
Output: [1,2]
Example 2:

Input: head = [1,1,2,3,3]
Output: [1,2,3]

Constraints:

The number of nodes in the list is in the range [0, 300].
-100 <= Node.val <= 100
The list is guaranteed to be sorted in ascending order.文章来源地址https://www.toymoban.com/news/detail-800802.html

二、题解

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(!head) return head;
        ListNode* cur = head;
        while(cur->next){
            if(cur->next->val == cur->val){
                cur->next = cur->next->next;
            }
            else cur = cur->next;
        }
        return head;
    }
};

到了这里,关于LeetCode83. Remove Duplicates from Sorted List的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【每日一题】Leetcode - 19. Remove Nth Node From End of List

    Leetcode - 19. Remove Nth Node From End of List Drawing on the method of finding midpoints in linked lists, use quick slow pointer Finding midpoints in linked lists nothing

    2024年02月12日
    浏览(39)
  • 算法: 移除单链表的倒数第N个节点 19. Remove Nth Node From End of List

    Given the head of a linked list, remove the nth node from the end of the list and return its head. Example 1: Example 2: Example 3: Constraints: The number of nodes in the list is sz. 1 = sz = 30 0 = Node.val = 100 1 = n = sz Follow up: Could you do this in one pass? 该方法采用了两次遍历链表的方法。首先,它计算链表的总长度,然后通

    2024年02月13日
    浏览(36)
  • python list.sort方法和内置函数sorted

    list.sort 方法会就地排序列表,也就是说不会把原列表复制一份。这也是这个方法的返回值是 None 的原因,提醒你本方法不会新建一个列表。在这种情况下返回 None 其实是Python 的一个惯例:如果一个函数或者方法对对象进行的是就地改动,那它就应该返回None,好让调用者知道

    2024年01月18日
    浏览(30)
  • JDK8的lambda方式List多字段排序List.stream().sorted()

    JDK8的lambda方式List多字段排序List.stream().sorted() 多字段排序 拼音排序: 引入jar包: 自定义比较器: 结束。

    2024年02月10日
    浏览(32)
  • 为什么list.sort()比Stream().sorted()更快?

    真的更好吗? 先简单写个demo 输出 由此可见list原生排序性能更好。 能证明吗? 证据错了。 再把demo变换一下,先输出stream.sort 此时输出变成了 这能证明上面的结论错误了吗? 都不能。 两种方式都不能证明什么。 使用这种方式在很多场景下是不够的,某些场景下,JVM会对代

    2024年02月14日
    浏览(26)
  • Java8-使用stream.sorted()对List排序

    1.流的定义 Stream 中文称为 “流”,通过将集合转换为这么一种叫做 “流” 的元素序列,通过声明性方式,能够对集合中的每个元素进行一系列并行或串行的操作! 如果流中的元素的类实现了 Comparable 接口,即有自己的排序规则,那么可以直接调用 sorted() 方法对元素进行排

    2024年02月16日
    浏览(37)
  • Arrays.asList() 返回的list不能add,remove

    Arrays.asList()返回的是List,而且是一个定长的List,所以不能转换为ArrayList,只能转换为AbstractList 原因在于asList()方法返回的是某个数组的列表形式,返回的列表只是数组的另一个视图,而数组本身并没有消失,对列表的任何操作最终都反映在数组上. 所以不支持remove,add方法的 下面是

    2024年02月13日
    浏览(45)
  • Redis之数据类型String、List、Hash、Set、Sorted Set(详细)

    一、String数据类型 1、SET/GET/APPEND/STRLEN (1) APPEND (2) SET/STRLEN 2、 INCR/ DECR/INCRBY/DECRBY (1)INCR/ DECR (2) INCRBY/DECRBY INCRBY key increment:key值增加指定的整数DECRBY key decrement:key值减少指定的整数  3、GETSET 4、 SETEX  5、SETNX 6、MSET/MGET/MSETNX  二、List数据类型 1、LPUSH/LPUSHX/

    2024年02月11日
    浏览(32)
  • LeetCode //88. Merge Sorted Array

    You are given two integer arrays nums1 and nums2 , sorted in non-decreasing order, and two integers m and n , representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1

    2024年02月12日
    浏览(37)
  • 为什么 list.sort() 比 stream().sorted() 要更快?测试结果把我惊呆了!

    作者:是奉壹呀 来源:juejin.cn/post/7262274383287500860 看到一个评论,里面提到了list.sort()和list.strem().sorted()排序的差异。 说到list sort()排序比stream().sorted()排序性能更好,但没说到为什么。 有朋友也提到了这一点。本文重新开始,先问是不是,再问为什么。 推荐一个开源免费的

    2024年02月09日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包