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
复制代码
复制代码
相关推荐
飞桨PaddlePaddle1 小时前
Wan2.1和HunyuanVideo文生视频模型算法解析与功能体验丨前沿多模态模型开发与应用实战第六期
人工智能·算法·百度·音视频·paddlepaddle·飞桨·deepseek
Starry_hello world2 小时前
C++ 快速幂算法
c++·算法·有问必答
石去皿3 小时前
力扣hot100 91-100记录
算法·leetcode·职场和发展
SsummerC4 小时前
【leetcode100】组合总和Ⅳ
数据结构·python·算法·leetcode·动态规划
尤物程序猿5 小时前
【2025面试Java常问八股之redis】zset数据结构的实现,跳表和B+树的对比
数据结构·redis·面试
2301_807611495 小时前
77. 组合
c++·算法·leetcode·深度优先·回溯
SsummerC6 小时前
【leetcode100】零钱兑换Ⅱ
数据结构·python·算法·leetcode·动态规划
好易学·数据结构7 小时前
可视化图解算法:二叉树的最大深度(高度)
数据结构·算法·二叉树·最大高度·最大深度·二叉树高度·二叉树深度
程序员-King.7 小时前
day47—双指针-平方数之和(LeetCode-633)
算法·leetcode
阳洞洞7 小时前
leetcode 1035. Uncrossed Lines
算法·leetcode·动态规划·子序列问题