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.

Input: head = [1,1,2]

Output: [1,2]

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

Output: [1,2,3]

给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。

python 复制代码
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if head is None:
            return None
        cur = head
        while cur.next:
            if cur.next.val == cur.val:
                cur.next = cur.next.next
            else:
                cur = cur.next
        return head
复制代码
复制代码
相关推荐
d111111111d36 分钟前
在STM32函数指针是什么,怎么使用还有典型应用场景。
笔记·stm32·单片机·嵌入式硬件·学习·算法
明洞日记41 分钟前
【数据结构手册008】STL容器完全参考指南
开发语言·数据结构·c++
kingmax542120081 小时前
《数据结构C语言:单向链表-链表基本操作(尾插法建表、插入)》15分钟试讲教案【模版】
c语言·数据结构·链表
AI科技星1 小时前
质量定义方程常数k = 4π m_p的来源、推导与意义
服务器·数据结构·人工智能·科技·算法·机器学习·生活
摇摆的含羞草1 小时前
哈希(hash)算法使用特点及常见疑问解答
算法·哈希算法
Fine姐2 小时前
数据结构04——二叉树搜索树BST
数据结构
仰泳的熊猫2 小时前
1077 Kuchiguse
数据结构·c++·算法·pat考试
LYFlied2 小时前
【每日算法】LeetCode 19. 删除链表的倒数第 N 个结点
算法·leetcode·链表
阿里巴巴AI编程社区2 小时前
Qoder 提效实战:数据开发工程师用 Qoder 提效50%
数据结构
踏浪无痕2 小时前
计算机算钱为什么会算错?怎么解决?
后端·算法·面试