当前位置: 首页 > 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 缓存

原题链接:

相关文章:

  • 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的使用)
  • dw下载手机版/seo搜索优化是什么呢
  • 电竞竞猜网站 建设/怎么免费制作网页
  • 做餐饮网站的目的与意义/互联网推广怎么找渠道
  • 深圳网站定制公司/百度seo和谷歌seo有什么区别
  • 连云港建设网站/seo怎么优化
  • 北京市朝阳区网站制作/如何做电商新手入门