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

leetcode -- 链表

时间:2022-09-27 00:45:16浏览次数:52  
标签:head ListNode -- next 链表 l2 l1 return leetcode

leetcode 链表专题

  1. 反转链表
三指针 + 递归
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        def reverselist(head: Optional[ListNode], prev: Optional[ListNode]):
            if head is None:
                return prev
            else:
                next = head.next
                head.next = prev
                return reverselist(next, head)

        return reverselist(head, None)

        prev, next = None, None
        #使用三指针写法 回收内存
        while head:
            next, head.next, prev = head.next, prev, head
            head = next
        del head, next
        return prev
  1. 合并两个有序链表(归并排序)
双指针迭代 + 递归链表
class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        # normal 两个链表叠加 merge
        dummy = ListNode()
        tail = dummy

        while l1 and l2:
            if l1.val <= l2.val:
                tail.next = l1
                l1 = l1.next
            else:
                tail.next = l2
                l2 = l2.next            
            tail = tail.next

        # 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
        tail.next = l1 if l1 is not None else l2
        return dummy.next

        if l1 is None:
            return l2
        if l2 is None:
            return l1

        if l1.val > l2.val:
            l2.next = self.mergeTwoLists(l1, l2.next)
            return l2
        else:
            l1.next = self.mergeTwoLists(l2, l1.next)
            return l1
  1. 两两交换链表中的节点
顺序遍历
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        p = head
        if p and p.next:
            s = p.next
            p.next, s.next = s.next, p
            head = s
            while p.next and p.next.next:
                s = p.next
                p.next = s.next
                s.next = p.next.next
                p.next.next = s
                p = s
        return head
  1. 相交链表
长度加和遍历 + set查表
class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
        # l1, l2 = headA, headB
        # while l1 is not l2:
        #     l1 = l1.next if l1 else headB
        #     l2 = l2.next if l2 else headA
        
        # return l1

        # 2
        lset = set()
        l1, l2 = headA, headB
        while l1:
            lset.add(l1)
            l1 = l1.next
        while l2:
            if l2 in lset:
                return l2
            else:
                l2 = l2.next
        return l2
  1. 回文链表
快慢指针 + 反转链表(不还原)
class Solution:
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        def reverseList(node: Optional[ListNode]) -> Optional[ListNode]:
            if node is None:
                return 
            prev, next = None, node.next
            while node:
                next = node.next
                node.next = prev
                prev, node = node, next
                
            return prev

            

        # main function
        if head is None or head.next is None:
            return True
        # 设置快慢指针 -> 截断链表并生成双子链表
        slow, fast = head, head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        
        node = reverseList(slow.next)
        slow.next = node
        slow = slow.next
        while slow:
            if head.val != slow.val:
                return False
            head = head.next
            slow = slow.next
        
        return True
  1. 删除排序链表中的重复元素
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        num_t = None
        ptr = head
        node = None
        while ptr:
            if ptr.val != num_t:
                num_t = ptr.val
                node = ptr
            else:
                node.next = ptr.next
                del ptr
            ptr = node.next

        return head
  1. 奇偶链表
双表链接
class Solution:
    def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        # 要求O(n)复杂度
        if not head:
            return
        
        odd_head = head
        evenHead, even_head = head.next, head.next
        while even_head and even_head.next:
            odd_head.next = even_head.next
            odd_head = odd_head.next
            even_head.next = odd_head.next
            even_head = even_head.next
        
        odd_head.next = evenHead
        return head
  1. 排序链表
归并排序 + 自底向上
class Solution:
    def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        # mergesort
        def mergesort(headA: Optional[ListNode], headB: Optional[ListNode]) -> ListNode:
            dummy= ListNode()
            tail = dummy
            l1, l2 = headA, headB
            while l1 and l2:
                if l1.val < l2.val:
                    tail.next = l1
                    l1 = l1.next
                else:
                    tail.next = l2
                    l2 = l2.next
                tail = tail.next
            tail.next = l1 if l1 else l2
            return dummy.next

        # main function 
        dummy = ListNode(next=head)
        length = 0
        
        # get the length
        node = head
        while node:
            node = node.next
            length += 1
        
        sublength = 1
        while sublength < length:
            p, c = dummy, dummy.next
            while c:
                # decide the head1
                head1 = c
                for _ in range(1, sublength):
                    if c.next:
                        c = c.next
                    else:
                        break
                # decide the head2
                head2 = c.next
                c.next = None
                c = head2
                for _ in range(1, sublength):
                    if c and c.next:
                        c = c.next
                    else:
                        break
                # save the next head
                succ = None
                if c:
                    succ = c.next
                    c.next = None

                p.next = mergesort(head1, head2)
                while p.next:
                    p = p.next
                c = succ
            sublength <<= 1
        return dummy.next

标签:head,ListNode,--,next,链表,l2,l1,return,leetcode
From: https://www.cnblogs.com/DocGu/p/16733102.html

相关文章

  • 关系数据库
    笛卡尔积每一组域的乘积==每一个组织之间相互的组合。其中的子集,不是所有的子集都具有实际意义码1.候选码(Candidatekey):某一个属性可以赢来作为这个数据的关键字,即,可以......
  • CSCN上的Mrakdown教程(Markdown基础用法)
    链接原文Markdown标题Markdown的标题有两种格式。使用=和-标记一级和二级标题我展示的是一级标题我展示的是二级标题1232.使用#号标记使用#号可表......
  • 代码随想录day6 | 242. 有效的字母异位词 349.两个数组的交集 202.快乐数1.两数之和
    242.有效的字母异位词题目|文章1.哈希思路字母的异位词意味着两个词中字符的种类和次数都相等。因为只有26个字符,所以我们可以通过数组实现一个26位的哈希表。先记录一......
  • 羊了个羊,但是Python简(li)单(pu)版
    大家好,欢迎来到Crossin的编程教室!要说最近最热门的游戏,那肯定是《羊了个羊》没跑了。连续上了好几天热搜,火到连央视都来提醒谨防有人利用游戏之名诈骗。但游戏爆火的另......
  • Javascript
    Javascript能够说出什么是编程语言能够区分编程语言和标记语言的不同能够说出常见的数据存储单位及其换算关系能够说出内存的主要作用以及特点编程语言02-编程语言_......
  • 浮点类型及使用细节
    单精度float双精度double浮点数再机器中存放形式简单说明,浮点数=符号位+指数位+尾数位尾数部分可能丢失,造成精度损失 1、java浮点类型有固定的范围和字段长度,......
  • 实验2:Open vSwitch虚拟交换机实践
    实验2:OpenvSwitch虚拟交换机实践一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Pytho......
  • 一文入门Qt Quick
    以下内容为本人的著作,如需要转载,请声明原文链接微信公众号「englyf」https://www.cnblogs.com/englyf/p/16733091.html初识QtQuick很高兴可以来到这一章,终于可以开始......
  • docker实战教程(十一):容器数据卷
    --privileged=truedocker挂载主机目录访问,如果出现cannotopendirectory:Permissiondenied解决办法:在挂载目录后多加一个--privileged=true参数即可如果是centos7安全......
  • Linux、Windows下Redis的安装即Redis的基本使用详解
    前言什么是RedisRedis是一个基于内存的key-value结构数据库。Redis是互联网技术领域使用最为广泛的存储中间件,它是「RemoteDictionaryService」的首字母缩写,也就......