首页 > 其他分享 >leetcode 83. Remove Duplicates from Sorted List

leetcode 83. Remove Duplicates from Sorted List

时间:2023-05-30 17:35:09浏览次数:36  
标签:node head ListNode val Duplicates self List Remove next

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.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        node = head
        while node:
            tmp = node
            val = node.val            
            while node.next and node.next.val == val:
                node = node.next
            # assert node.next is None or node.next.val != val
            tmp.next = node.next
            node = node.next
        return head

 

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        dummy = cur = ListNode(None)
        node = head
        while node:
            if node.val != cur.val:
                cur.next = node
                cur = cur.next
            node = node.next
        cur.next = None
        return dummy.next

 

发现其他人的解法和我的都不一样,每次上一个节点比较,如果数值相同,则直接删除当前节点。

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head: return None      
        node = head
        while node.next:
            if node.next.val == node.val:
                node.next = node.next.next
            else:
                node = node.next
        return head

 递归解法:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head: return None              
        if not head.next: return head        
        head.next = self.deleteDuplicates(head.next)
        if head.val == head.next.val:
            return head.next
        else:
            return head

 

标签:node,head,ListNode,val,Duplicates,self,List,Remove,next
From: https://blog.51cto.com/u_11908275/6381010

相关文章

  • leetcode 27. Remove Element
    Givenanarraynumsandavalueval,removeallinstancesofthatvaluein-placeandreturnthenewlength.Donotallocateextraspaceforanotherarray,youmustdothisbymodifyingtheinputarrayin-placeTheorderofelementscanbechanged.Itdoesn&......
  • C# 程序开发中如何移除List集合的某列(属性)呢?
    如题,在C#&.NET,.NETCore程序开发中如何移除List集合的某列(属性)呢?比如,有以下的MyClass类: publicclassMyClass{publicintColumn1{get;set;}publicstringColumn2{get;set;}publicintColumn3{get;set;}}现在MyClass的集合myList,如何......
  • linkedList
    importjava.util.LinkedList;importjava.util.List;publicclassLinkedL{publicstaticvoidmain(String[]args){//LinkedList//1--创建对象LinkedListlinkedList=newLinkedList();//2--调用方法//add(是list的子类所以说还......
  • 服务之:urlacl解决服务HttpListener监听url需要管理员
    在使用HttpListener监听url时,如果没有以管理员身份运行可能会抛出异常,无法监听,遇到这个问题时,可以先使用管理员权限的程序将Url注册到urlacl列表,解决普通权限无法开启监听问题第一步: Netshhttpshowurlacl。查看有没有需要监听的url 第二步:添加Url到Urlacl:例如添加  "......
  • 3.4. Java集合框架(List、Set、Map等)
    Java集合框架是Java提供的一套用于存储和操作数据的接口和类。它包括以下几个主要部分:接口:集合框架定义了一系列接口,如Collection、List、Set、Map等。实现类:集合框架提供了一些实现这些接口的类,如ArrayList、LinkedList、HashSet、LinkedHashSet、HashMap、LinkedHashMap等。......
  • 武汉星起航:亚马逊卖家优化产品Listing,轻松提升产品排名
    在竞争激烈的亚马逊市场中,卖家们都希望能够提高产品的排名,获得更多的曝光和销售机会。而优化产品的Listing是实现这一目标的关键步骤。武汉星起航在这里提供了一些亚马逊卖家有效的优化技巧,让他们的产品在激烈的竞争中脱颖而出。首先,关键词是优化产品Listing的重要元素。在撰写产品......
  • C# MVC pagedlist 分页实现
    1.安装组件PagedList.Mvc:包含PagedList、PagedList.Mvc,反过来先安装组件PagedList,安装时不会同时安装PagedList.Mvc2.Web.config配置分页条数<appSettings><!--分页条件:每页显示的记录数--><addkey="pageSize"value="5"/></appSettings>3.cont......
  • 使用 Collections中的replaceAll方法 替换list中的指定元素
    以下实例演示了如何使用Collections类的replaceAll()来替换List中所有的指定元素:importjava.util.Arrays;importjava.util.Collections;importjava.util.List;publicclassImoocStudent{publicstaticvoidmain(String[]args)throwsException{......
  • Collections.rotate 对List集合进行循环移动操作
    它的作用是将列表中的元素向右移动指定的距离,如果移动的距离是负数,则表示向左移动。方法的声明如下:publicstaticvoidrotate(List<?>list,intdistance)其中,list是要进行移动的List集合,distance是指移动的距离,可以是正数或负数。demo1importjava.util.Arrays;im......
  • 【python】内置函数list
    内置函数listlist()方法用于将元组转换为列表。注:元组与列表是非常类似的,区别在于元组的元素值不能修改,元组是放在括号中,列表是放于方括号中。#!/usr/bin/python#-*-coding:UTF-8-*-aTuple=(123,'runoob','google','abc');aList=list(aTuple)print("列表......