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
复制代码
复制代码
相关推荐
ZZZS05166 分钟前
stack栈练习
c++·笔记·学习·算法·动态规划
hans汉斯32 分钟前
【人工智能与机器人研究】基于力传感器坐标系预标定的重力补偿算法
人工智能·算法·机器人·信号处理·深度神经网络
vortex52 小时前
算法设计与分析:分治、动态规划与贪心算法的异同与选择
算法·贪心算法·动态规划
前端拿破轮2 小时前
🤡🤡🤡面试官:就你这还每天刷leetcode?连四数相加和四数之和都分不清!
算法·leetcode·面试
地平线开发者3 小时前
征程 6|工具链量化简介与代码实操
算法·自动驾驶
DoraBigHead3 小时前
🧠 小哆啦解题记——谁偷改了狗狗的台词?
算法
Kaltistss3 小时前
240.搜索二维矩阵Ⅱ
线性代数·算法·矩阵
轻语呢喃3 小时前
每日LeetCode:合并两个有序数组
javascript·算法
大熊猫侯佩4 小时前
Swift 数学计算:用 Accelerate 框架让性能“加速吃鸡”
算法·swift
杰克尼4 小时前
2. 两数相加
算法