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
复制代码
复制代码
相关推荐
budingxiaomoli1 天前
算法--双指针二
算法
做科研的周师兄1 天前
【机器学习入门】8.2 主成分分析:一文吃透主成分分析(PCA)—— 从原理到核心逻辑
人工智能·算法·决策树·机器学习·流程图
LeeZhao@1 天前
【具身智能】具身机器人VLA算法入门及实战(四):具身智能VLA技术行业进展
人工智能·算法·机器人
lingchen19061 天前
矩阵的除法
人工智能·算法·矩阵
liu****1 天前
笔试强训(六)
数据结构·c++·算法
草莓工作室1 天前
数据结构5:线性表5-循环链表
数据结构·循环链表
前端小刘哥1 天前
超越“接收端”:解析视频推拉流EasyDSS在RTMP推流生态中的核心价值与中流砥柱作用
算法
前端小刘哥1 天前
新版视频直播点播平台EasyDSS用视频破局,获客转化双提升
算法
海琴烟Sunshine1 天前
leetcode 168. Excel 表列名称 python
python·算法·leetcode
京东零售技术1 天前
探索无限可能:生成式推荐的演进、前沿与挑战
算法