首页 > 其他分享 >leetcode -- 链表 2

leetcode -- 链表 2

时间:2022-09-27 10:49:07浏览次数:71  
标签:node head ListNode -- next 链表 tail return leetcode

leetcode链表专题
23. 合并K个升序链表

普通归并排序 + python迭代器
class Solution:
    def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        def merge(head1: Optional[ListNode], head2: Optional[ListNode]) -> Optional[ListNode]:
            if not head1:
                return head2
            if not head2:
                return head1
            
            dummy = ListNode()
            tail = dummy
            
            while head1 and head2:
                if head1.val <= head2.val:
                    tail.next = head1
                    head1 = head1.next
                else:
                    tail.next = head2
                    head2 = head2.next
                tail = tail.next
            tail.next = head1 if head1 else head2
            
            return dummy.next
        
        it = iter(lists)
        dummy = ListNode()
        while it:
            try:
                dummy.next = merge(dummy.next, next(it))
            except StopIteration:
                break
        
        return dummy.next
  1. K 个一组翻转链表
反转链表 + 指针
class Solution:
    def reverse(self, head: ListNode, tail: ListNode):
        prev = tail.next
        p = head
        while prev != tail:
            nex = p.next
            p.next = prev
            prev = p
            p = nex
        return tail, head

    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        dummy = ListNode(0, head)
        pre = dummy

        while head:
            tail = pre
            
            for i in range(k):
                tail = tail.next
                if not tail:
                    return dummy.next
                
            nex = tail.next
            head, tail = self.reverse(head, tail)
            
            pre.next = head
            tail.next = nex
            pre = tail
            head = tail.next
        
        return dummy.next
  1. 旋转链表
约束条件多的一批
class Solution:
    def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        if not head:
            return
        node, length = head, 0
        while node:
            length += 1
            node = node.next
        k = k%length if k >= length else k
        
        fast, slow = head, head
        while k:
            fast = fast.next
            k -= 1
        
        while fast.next:
            fast = fast.next
            slow = slow.next
        
        if slow == fast:
            return head
        
        node = slow.next
        slow.next = None
        fast.next = head
        return node
  1. 删除排序链表中的重复元素 II
双指针
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode(None, next=head)
        node = dummy
        while node.next:
            cur = node.next
            while cur.next and cur.val == cur.next.val:
                cur = cur.next
            if cur == node.next:
                node = cur
            else:
                node.next = cur.next
        return dummy.next

标签:node,head,ListNode,--,next,链表,tail,return,leetcode
From: https://www.cnblogs.com/DocGu/p/16733657.html

相关文章

  • IDEA控制台输出中文为乱码解决方法
    IDEA控制台输出中文为乱码解决方法 当前环境:IntelliJIDEA2021.3heJDK18 01)在IDEA设置中修改编码设置  02)修改IDEA安装文件idea64.exe.vmoptions修改-......
  • mysql5.7 分配子账户和解决进程错误
    mysql5.7和5.6还是有稍微的区别,关键点在于5.7分配子账户之后需要分配进程权限。否则navicate点击表设计报错。//1.mysql显示所有的创建的用户:SELECTDISTINCTCONCAT('......
  • 界面组件DevExpress WinForms v22.1 - 全新升级的HTML CSS 模板
    DevExpressWinForms拥有180+组件和UI库,能为WindowsForms平台创建具有影响力的业务解决方案。DevExpressWinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office......
  • @SuppressWarnings
    简介java.lang.SuppressWarnings是J2SE5.0中标准的Annotation之一。可以标注在类、字段、方法、参数、构造方法,以及局部变量上。作用告诉编译器忽略指定的警告,不用在编......
  • Amazon linux docker安装
    Amazon的服务器安装docker和普通Linux系统安装有些许的区别,安装源可以使用Amazon的1、安装Docker#安装步骤sudoyuminstall-yamazon-linux-extrasyum-utilsdevice......
  • WINZIP命令行
    来自:https://www.cnblogs.com/nieyj/archive/2009/08/18/1548727.htmlWINZIP命令行在WinZip安装包中没有包含命令行工具,但在其官方站点上有单独提供下载,所以如需要WinZi......
  • git 查看代码是谁提交的
    目录git查看代码是谁提交的用idea查看git查看代码是谁提交的有很多种方法,比如登陆gitlab查看,或者在本地查看等用idea查看当我们拉取代码后,本地代码或者脚本无法定位......
  • Convert gif to Base64 String Using JavaScript
    letxhRequest=newXMLHttpRequest();xhRequest.onload=function(){letreader=newFileReader();reader.onloadend=function(){......
  • 浙里办调试接口总结
    publicRzlbLoginnew(Stringticket){StringSecretKey="xxx";StringAccessKey="xxx";Stringurl="浙里办个人登陆获取信息地址IC33xxx02";......
  • 归并排序
    #include<iostream>usingnamespacestd;voidmerge(inta[],ints1,inte1,ints2,inte2){ intn1=e1-s1+1; intn2=e2-s2+1; intal[n1]; int......