首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >leetcode: 82. Remove Duplicates from Sorted List II

leetcode: 82. Remove Duplicates from Sorted List II

作者头像
JNingWei
发布于 2018-09-27 09:02:23
发布于 2018-09-27 09:02:23
28200
代码可运行
举报
文章被收录于专栏:JNing的专栏JNing的专栏
运行总次数:0
代码可运行

Problem

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Given a sorted linked list, delete all nodes that have duplicate numbers,
# leaving only distinct numbers from the original list.
#
# For example,
# Given 1->2->3->3->4->4->5, return 1->2->5.
# Given 1->1->1->2->3, return 2->3.

AC

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class ListNode():
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution():
    def deleteDuplicates(self, head):
        pre = dummy = ListNode(0)
        cur = head
        while cur:
            if cur.next and cur.next.val == cur.val:
                val = cur.val
                while cur and cur.val == val:
                    cur = cur.next
                pre.next = cur
            else:
                pre.next, pre, cur = cur, cur, cur.next
        return dummy.next


if __name__ == "__main__":
    head, head.next, head.next.next, head.next.next.next, head.next.next.next.next, head.next.next.next.next.next, head.next.next.next.next.next.next\
        = ListNode(1), ListNode(2), ListNode(3), ListNode(3), ListNode(4), ListNode(4), ListNode(5)
    print(Solution().deleteDuplicates(head))
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年11月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
leetcode: 61. Rotate List
Problem # Given a list, rotate the list to the right by k places, where k is non-negative. # # Example: # Given 1->2->3->4->5->NULL and k = 2, # return 4->5->1->2->3->NULL. AC class ListNode: def __init__(self, x): self.val = x self
JNingWei
2018/09/27
4460
Leetcode 82 Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2->3. 删除链表中重复出现的节点,和83题类似 http://blog.
triplebee
2018/01/12
5700
Leetcode 题目解析之 Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
ruochen
2022/01/14
1.2K0
【Leetcode】82. 删除排序链表中的重复元素 II
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
Leetcode名企之路
2018/10/25
6310
【Leetcode】82. 删除排序链表中的重复元素 II
leetcode: 83. Remove Duplicates from Sorted List
Problem # Given a sorted linked list, delete all duplicates such that each element appear only once. # # For example, # Given 1->1->2, return 1->2. # Given 1->1->2->3->3, return 1->2->3. AC class ListNode(): def __init__(self, x): self.val
JNingWei
2018/09/27
3660
[LeetCode] 82. Remove Duplicates from Sorted List II
该文讲述了如何删除排序链表中的重复节点,并保留非重复节点。通过先构建一个虚拟头节点来处理头结点,然后遍历链表,如果当前节点和下一个节点的值相同,则删除当前节点,否则将当前节点和下一个节点连接起来。遍历结束后,返回虚拟头节点的下一个节点即可。该解法使用了递归和虚拟头节点,具有较高的效率和可读性。
用户1148830
2018/01/04
7150
LinkedList - 82. Remove Duplicates from Sorted List II
82. Remove Duplicates from Sorted List II
ppxai
2020/09/23
3320
leetcode: 92. Reverse Linked List II
Problem # Reverse a linked list from position m to n. Do it in-place and in one-pass. # # For example: # Given 1->2->3->4->5->NULL, m = 2 and n = 4, # # return 1->4->3->2->5->NULL. # # Note: # Given m, n satisfy the following condition: # 1 ≤ m ≤ n ≤ le
JNingWei
2018/09/27
3350
Leetcode【61、82、83、142、143、1171】
1、先计算链表长度 size,k = k % size,如果 k % size == 0,则不用移动,直接返回 head; 2、否则,需要将前 size - k 个结点移动到后面。因此只需要循环 size - k 次,找到新链表头部,然后进行指针的交换。最后返回新链表头即可。
echobingo
2019/10/29
5330
leetcode 83 Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
流川疯
2019/01/18
4080
Q83 Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 解题思路: 链表删除操作的应用,即 cur.next = cur.next.next,时间复杂度为 O(n)。 Python实现: # Definition for si
echobingo
2018/04/25
7380
leetcode: 19. Remove Nth Node From End of List
Problem # Given a linked list, remove the nth node from the end of list and return its head. # # For example, # # Given linked list: 1->2->3->4->5, and n = 2. # # After removing the second node from the end, the linked list becomes 1->2->3->5. # # Note
JNingWei
2018/09/28
2750
leetcode: 86. Partition List
Problem # Given a linked list and a value x, # partition it such that all nodes less than x come before nodes greater than or equal to x. # # You should preserve the original relative order of the nodes in each of the two partitions. # # For example, #
JNingWei
2018/09/27
4190
83. Remove Duplicates from Sorted List(Linked List-Easy)
该文讲述了如何删除排好序的链表中的重复元素,使得每个元素只出现一次。首先,定义一个虚拟头节点,然后遍历链表,如果当前节点和下一个节点的值相同,则将下一个节点的next指针指向当前节点的next指针,最后返回虚拟头节点的下一个节点即可。对于给定的链表1->1->2,返回的是1->2;对于链表1->1->2->3->3,返回的是1->2->3。
Jack_Cui
2018/01/08
5810
LeetCode 0082 - Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Reck Zhang
2021/08/11
1820
LeetCode每日一题-6:k个一组翻转链表
你的算法只能使用常数的额外空间。 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
墨明棋妙27
2022/09/23
3320
leetcode: 23. Merge k Sorted Lists
具体原因见我的另一篇博客:python3 调用heapq库 时遭遇 “TypeError: unorderable types”
JNingWei
2018/09/28
5610
leetcode: 23. Merge k Sorted Lists
2021-03-14:手写代码:单链表冒泡排序。
遍历链表,算出元素个数,假设为N。需要嵌套循环,外循环N-1轮,每轮循环相邻交换N-1次。
福大大架构师每日一题
2021/03/14
4220
Leetcode【86、92、148、206】
这道题是给一个链表和整数 x,将小于 x 的数按位置顺序放在链表左侧,大于等于 x 的按位置顺序放在右侧。
echobingo
2019/10/29
4320
LEETCODE - Linked List 题目思路汇总
浏览了一下 Leetcode 上 Linked List 的题目,我把它分为 6 类: 调换顺序 删除 合并 环 变身 复制 做Leetcode还是要归类总结才好玩,最开始做两三个觉得很懵,做四五个就
杨熹
2018/04/02
7130
LEETCODE - Linked List 题目思路汇总
相关推荐
leetcode: 61. Rotate List
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档