当前位置: 首页 > news >正文

LeetCode Hot100 刷题笔记(3)—— 链表

目录

前言

1. 相交链表

2. 反转链表

3. 回文链表

4. 环形链表

5. 环形链表 II

6. 合并两个有序链表

7. 两数相加

8. 删除链表的倒数第 N 个结点

9. 两两交换链表中的节点

10. K 个一组翻转链表

11. 随机链表的复制

12. 排序链表

13. 合并 K 个升序链表

14. LRU 缓存


前言

一、链表:相交链表,反转链表,回文链表,环形链表,环形链表 II,合并两个有序链表,两数相加,删除链表的倒数第 N 个结点,两两交换链表中的节点,K 个一组翻转链表,随机链表的复制,排序链表,合并 K 个升序链表,LRU 缓存。(日更中...)

*** Trick:本质将链表转为list,再在list上进行操作,最后转回链表。

*** Trick 通用模版

class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution(object):
    def Operation(self, head):
        if not head:
            return None
        lst = []
        while head:
            lst.append(head.val)
            head = head.next
        """
        系列操作
        """
        head_new = ListNode(int(lst[0]))
        curr = head_new
        for v in lst[1:]:
            curr.next = ListNode(int(v))
            curr = curr.next
        return head_new

1. 相交链表

原题链接:160. 相交链表 - 力扣(LeetCode)

class Solution(object):
    def getIntersectionNode(self, headA, headB):
        set1 = set()
        while headA:
            set1.add(headA)
            headA = headA.next
        while headB:
            if headB in set1:
                return headB
            headB = headB.next
        return None

2. 反转链表

原题链接:206. 反转链表 - 力扣(LeetCode)

class Solution(object):
    def reverseList(self, head):
        if not head:
            return None
        lst = []
        while head:
            lst.append(head.val)
            head = head.next
        lst.reverse()

        head_new = ListNode(int(lst[0]))
        curr = head_new
        for v in lst[1:]:
            curr.next = ListNode(int(v))
            curr = curr.next
        return head_new

3. 回文链表

原题链接:234. 回文链表 - 力扣(LeetCode)

class Solution(object):
    def isPalindrome(self, head):
        if not head:
            return None
        lst = []
        while head:
            lst.append(head.val)
            head = head.next
        if lst == lst[::-1]:
            return True
        return False

4. 环形链表

原题链接:141. 环形链表 - 力扣(LeetCode)

class Solution(object):
    def hasCycle(self, head):
        if not head:
            return False
        set1 = set()
        while head:
            set1.add(head)
            head = head.next
            if head in set1:
                return True
        return False

5. 环形链表 II

原题链接:142. 环形链表 II - 力扣(LeetCode)

class Solution(object):
    def detectCycle(self, head):
        if not head:
            return None
        set1 = set()
        while head:
            set1.add(head)
            head = head.next
            if head in set1:
                return head
        return None

6. 合并两个有序链表

原题链接:21. 合并两个有序链表 - 力扣(LeetCode)

class Solution(object):
    def mergeTwoLists(self, list1, list2):
        if not list1 and not list2:
            return None
        elif not list1:
            return list2
        elif not list2:
            return list1
        else:
            lst1, lst2 = [], []
            while list1:
                lst1.append(list1.val)
                list1 = list1.next
            while list2:
                lst2.append(list2.val)
                list2 = list2.next
            lst = lst1 + lst2
            lst.sort()

            head = ListNode(int(lst[0]))
            curr = head
            for v in lst[1:]:
                curr.next = ListNode(int(v))
                curr = curr.next
            return head

7. 两数相加

原题链接:2. 两数相加 - 力扣(LeetCode)

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        lst1, lst2 = [], []
        while l1:
            lst1.append(l1.val)
            l1 = l1.next
        while l2:
            lst2.append(l2.val)
            l2 = l2.next
        # s1 = ''.join(l1)
        # s2 = ''.join(l2)
        lst1.reverse()
        lst2.reverse()
        s1 = ''.join([str(i) for i in lst1])
        s2 = ''.join([str(i) for i in lst2])
        s3 = int(s1) + int(s2)
        lst3 = list(str(s3))
        lst3.reverse()

        head = ListNode(int(lst3[0]))
        curr = head
        for v in lst3[1:]:
            curr.next = ListNode(int(v))
            curr = curr.next
        return head
        

8. 删除链表的倒数第 N 个结点

原题链接:19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

class Solution(object):
    def removeNthFromEnd(self, head, n):
        lst = []
        while head:
            lst.append(head.val)
            head = head.next
        del lst[-n]
        if not lst:
            return None
        else:
            head_new = ListNode(lst[0])
            curr = head_new
            for v in lst[1:]:
                curr.next = ListNode(v)
                curr = curr.next
            return head_new

9. 两两交换链表中的节点

原题链接:24. 两两交换链表中的节点 - 力扣(LeetCode)

class Solution(object):
    def swapPairs(self, head):
        if not head:
            return None
        lst = []
        while head:
            lst.append(head.val)
            head = head.next
        for i in range(0, len(lst)-1, 2):
            lst[i], lst[i+1] = lst[i+1], lst[i]
        head_new = ListNode(lst[0])
        curr = head_new
        for value in lst[1:]:
            curr.next = ListNode(value)
            curr = curr.next
        return head_new

10. K 个一组翻转链表

原题链接:25. K 个一组翻转链表 - 力扣(LeetCode)

class Solution(object):
    def reverseKGroup(self, head, k):
        lst = []
        while head:
            lst.append(head.val)
            head = head.next
        for i in range(0, len(lst)-k+1, k):
            lst[i:i+k] = lst[i:i+k][::-1]

        head_new = ListNode(lst[0])
        curr = head_new
        for v in lst[1:]:
            curr.next = ListNode(v)
            curr = curr.next
        return head_new

11. 随机链表的复制

原题链接:138. 随机链表的复制 - 力扣(LeetCode)

class Node:
    def __init__(self, x, next=None, random=None):
        self.val = int(x)
        self.next = next
        self.random = random


class Solution(object):
    def copyRandomList(self, head):
        return copy.deepcopy(head)

12. 排序链表

原题链接:148. 排序链表 - 力扣(LeetCode)

class Solution(object):
    def sortList(self, head):
        if not head:
            return None
        lst = []
        while head:
            lst.append(head.val)
            head = head.next
        lst.sort()
         
        head_new = ListNode(int(lst[0]))
        curr = head_new
        for v in lst[1:]:
            curr.next = ListNode(int(v))
            curr = curr.next
        return head_new

13. 合并 K 个升序链表

原题链接:23. 合并 K 个升序链表 - 力扣(LeetCode)

class Solution(object):
    def mergeKLists(self, lists):
        if not lists:
            return None
        lst1 = []
        for head in lists:
            lst2 = []
            while head:
                lst2.append(head.val)
                head = head.next
            lst1.append(lst2)
        lst1 = sum(lst1, [])  
        if not lst1:
            return None      # lst1 = [[]]

        lst1.sort()
        head_new = ListNode(lst1[0])
        curr = head_new
        for v in lst1[1:]:
            curr.next = ListNode(v)
            curr = curr.next
        return head_new

14. LRU 缓存

原题链接:


文章转载自:

http://kSipDOKT.qwrqg.cn
http://X9xEWaVP.qwrqg.cn
http://IVVh8ugC.qwrqg.cn
http://Ke445bUK.qwrqg.cn
http://I9yUFfFm.qwrqg.cn
http://Qx5OWiBr.qwrqg.cn
http://OvRriBfp.qwrqg.cn
http://vcJCHVDj.qwrqg.cn
http://ilUenzXP.qwrqg.cn
http://Pi456Jj3.qwrqg.cn
http://CFmgcaY5.qwrqg.cn
http://iFpWfTaY.qwrqg.cn
http://g1CBPDvy.qwrqg.cn
http://Om1l5Xds.qwrqg.cn
http://VEUn5b1f.qwrqg.cn
http://CCsdF7vJ.qwrqg.cn
http://JtrUYuxw.qwrqg.cn
http://NK8mtVCX.qwrqg.cn
http://qeWtqp5S.qwrqg.cn
http://DFxYvh8x.qwrqg.cn
http://ZrXplgki.qwrqg.cn
http://M1iAChxJ.qwrqg.cn
http://OxT9TTxg.qwrqg.cn
http://nbAmmq12.qwrqg.cn
http://2Vjz9rR2.qwrqg.cn
http://YotLrLah.qwrqg.cn
http://TmMxkUY6.qwrqg.cn
http://Nxpzeuzr.qwrqg.cn
http://AOevhNuD.qwrqg.cn
http://751WFMwi.qwrqg.cn
http://www.dtcms.com/a/116243.html

相关文章:

  • Python作业2 蒙特卡罗方法手搓图形
  • 使用 VIM 编辑器对文件进行编辑
  • 路由器学习
  • 【C++奇遇记】C++中的进阶知识(多态(一))
  • 使用MySQL时出现 Ignoring query to other database 错误
  • NO.65十六届蓝桥杯备战|基础算法-贪心推公式排序|哈夫曼编码|拼数|奶牛玩杂技|哈夫曼编码|合并果子(C++)
  • 接口自动化学习二:session自动管理cookie
  • 网络协议:TCP,UDP详细介绍
  • Windows Flip PDF Plus Corporate PDF翻页工具
  • MySQL数据库精研之旅第五期:CRUD的趣味探索(中)
  • py文件打包为exe可执行文件,涉及mysql连接失败以及找不到json文件
  • 使用PyQt5绘制水波浪形的柱状显示流量—学习QTimer+QPainterPath
  • Logo语言的区块链
  • Compose组件转换XML布局1.0
  • 基于SpringBoot的医院信息管理系统(源码+数据库)
  • 基于Python的人脸识别校园考勤系统
  • 初见TypeScript
  • 微信小程序—路由
  • Qt 入门 0 之 QtCreator 简介
  • 【微服务架构】SpringCloud Alibaba(八):Nacos 2.1.0 作为配置中心(Nacos的使用)
  • SpringAI+MCP协议 实战
  • 第六章:框架实战:构建集成向量与图谱的 RAG 应用
  • Qt的window注册表读写以及删除
  • VBA数据库解决方案第二十讲:SQL在VBA中几种常见的表达方式
  • 11.使用依赖注入容器实现松耦合
  • Gerapy二次开发:git项目实现变更拉取与上传
  • 蓝桥与力扣刷题(74 搜索二维矩阵)
  • Docker基础1
  • Jetpack Compose 生产级语言切换架构:ViewModel + Flow 的终极实现
  • 今是科技发布全新测序仪G-seq1M:以效率与精准引领基因测序新标杆