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

leetcode: 83. Remove Duplicates from Sorted List

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

Problem

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# 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

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

class Solution():
    def deleteDuplicates(self, head):
        cur = head
        while cur:
            while cur.next and cur.next.val == cur.val:
                cur.next = cur.next.next
            cur = cur.next
        return head


if __name__ == "__main__":
    head, head.next, head.next.next, head.next.next.next, head.next.next.next.next \
        = ListNode(1), ListNode(1), ListNode(2), ListNode(3), ListNode(3)
    assert Solution().deleteDuplicates(head).val == 1
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表: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
4450
leetcode-83-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. 要完成的函数: struct ListNode {   int val;   ListNode *next;   ListNode(int x) : val(x
chenjx85
2018/05/21
4900
leetcode: 82. Remove Duplicates from Sorted List II
Problem # 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 class ListNo
JNingWei
2018/09/27
2820
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
7370
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
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
5780
【Leetcode】82. 删除排序链表中的重复元素 II
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
Leetcode名企之路
2018/10/25
6300
【Leetcode】82. 删除排序链表中的重复元素 II
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
4180
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
5320
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
2720
LeetCode-83-删除排序链表中的重复元素
初始化1个指针,指向头部,判断后一个数和前一个是不是相等,相等则要把后面一个数覆盖前面一个数,当发现不相等时,cur指针顺移1位,即对于1、1、2、3、3这样的数据,cur会将后一个重复的数字替换前一个重复数字,当重复数字之后一位数不和当前相等时,cur指针改变指向到下一个数,再进行重复判断。
benym
2022/07/14
1650
画解算法:83. 删除排序链表中的重复元素
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
灵魂画师牧码
2019/06/27
3780
画解算法:83. 删除排序链表中的重复元素
[LeetCode] 82. Remove Duplicates from Sorted List II
该文讲述了如何删除排序链表中的重复节点,并保留非重复节点。通过先构建一个虚拟头节点来处理头结点,然后遍历链表,如果当前节点和下一个节点的值相同,则删除当前节点,否则将当前节点和下一个节点连接起来。遍历结束后,返回虚拟头节点的下一个节点即可。该解法使用了递归和虚拟头节点,具有较高的效率和可读性。
用户1148830
2018/01/04
7150
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
3330
LinkedList - 82. Remove Duplicates from Sorted List II
82. Remove Duplicates from Sorted List II
ppxai
2020/09/23
3290
Leetcode 83 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. 删除链表中的重复元素,快慢指针解决! /** * Definition for singly-linked list. * struct ListNode {
triplebee
2018/01/12
5650
LinkedList - 83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
ppxai
2020/09/23
3080
leetcode: 23. Merge k Sorted Lists
具体原因见我的另一篇博客:python3 调用heapq库 时遭遇 “TypeError: unorderable types”
JNingWei
2018/09/28
5600
leetcode: 23. Merge k Sorted Lists
leetcode: 24. Swap Nodes in Pairs
Problem # Given a linked list, swap every two adjacent nodes and return its head. # # For example, # Given 1->2->3->4, you should return the list as 2->1->4->3. # # Your algorithm should use only constant space. # You may not modify the values in the li
JNingWei
2018/09/28
3580
LeetCode笔记:83. Remove Duplicates from Sorted List
既然链表本身已经排好序了,那么只用比较当前位置的值和next的值是否一样,一样就把next指向下一个再继续判断就好了,思路还是比较简单,但是有几个容易忽略的点需要注意。
Cloudox
2021/11/23
1400
相关推荐
leetcode: 61. Rotate List
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档