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

Remove Duplicates from Sorted List

时间:2023-06-21 12:22:04浏览次数:44  
标签:head cur Duplicates list List next Output Sorted Input

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.

Example 1:

Input: head = [1,1,2]
Output: [1,2]

Example 2:

Input: head = [1,1,2,3,3]
Output: [1,2,3]

Solution:

class Solution(object):
    def deleteDuplicates(self, head):
        if not head:
            return head

        cur = head # 指针,用来串联不重复的元素
        while cur.next:
            if cur.val == cur.next.val:
                cur.next = cur.next.next
            else:
                cur = cur.next

        return head

标签:head,cur,Duplicates,list,List,next,Output,Sorted,Input
From: https://www.cnblogs.com/artwalker/p/17495966.html

相关文章

  • Springboot api的controller如何接口一个List<Object>参数
    1.正常情况下,你可能会这样写:@PostMapping("/delete")@ApiOperation("Deletelistdata")@ResponseStatus(HttpStatus.OK)@ResponseBodypublicDBUpdateStatusdeleteTestCaseDatas(List<TestCaseInfo>testCaseInfoList){......
  • listView获得CheckBox 的位置
    当一个lsitView中每个ietm有多个控件的时候想获得其中的位置,其实主要是item的位置@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){//grabviewViewv=convertView;//setviewlayoutif(v==null){LayoutInflatervi=......
  • listView中item 图文并存的两种方法
    1.<?xmlversion="1.0"encoding="utf-8"?><TextViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@android:id/text1"android:layout_width="fill_parent"......
  • 【whale-starry-stl】01天 list学习笔记
    一、知识点1.std::bidirectional_iterator_tagstd::bidirectional_iterator_tag是C++标准库中定义的一个迭代器类型标签,用于标识支持双向遍历的迭代器类型。在C++中,迭代器是一种泛型指针,用于遍历容器中的元素。迭代器类型标签用于标识迭代器的特性,从而在算法中选择合适的......
  • listView显示选择图片
    publicclassItemsListextendsListActivity{privateItemsAdapteradapter;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.items_list);this.adapter=......
  • 武汉星起航:亚马逊卖家的优质关键词获取与产品Listing优化
    在亚马逊平台上,作为卖家,通过优质的关键词来优化产品Listing是提高曝光度和销售额的重要策略。本文将介绍一些方法,帮助亚马逊卖家获取优质关键词,并实施产品Listing的优化,以提升产品的销售业绩。一、市场调研和竞争分析在选择关键词之前,卖家应进行市场调研和竞争分析。了解目标市场的......
  • 20230313 java.util.LinkedList
    简介java.util.LinkedListLinkedList相对ArrayList要复杂一些,不是因为链表操作比数组操作复杂,而是LinkedList实现了更多接口LinkedList除了实现List接口外,还实现了Queue和Deque接口,也就意味着可以作为队列或双向队列使用对链表的学习非常有帮助感悟对于接口的认......
  • PAT_Advanced Level_1083 List Grades (25分)(C++_快排)
    GivenalistofNstudentrecordswithname,IDandgrade.Youaresupposedtosorttherecordswithrespecttothegradeinnon-increasingorder,andoutputthosestudentrecordsofwhichthegradesareinagiveninterval.InputSpecification:Eachinput......
  • 1.redis常见数据类型-字符串String、列表List、集合Set、Hash哈希、Zset有序集合
    背景:这里说的数据类型是value的数据类型,key的类型都是字符串。命令不区分大小写,而key的值是区分大小写的 help@+数据类型会出现命令提示比如help@string,help@list常见命令:keys*查看当前库所有key(匹配:keys*1)existskey判断某个key是否存在typekey查看你的......
  • My id 0 not in the peer list
    今天重启zookeeper爆了个错Myid0notinthepeerlist#######################cluster##########################server.131=192.168.10.131:2888:3888server.132=192.168.10.132:2888:3888server.133=192.168.10.133:2888:3888配置参数解读server.A=B:C:D。A是一个......