代码随想录算法训练营day4(链表)

华子目录

两两交换链表中的节点

思路

  • 使用虚拟头结点(要操作连续的两个节点,就必须找到其上一个节点),cur指向该节点
  • 在交换之前,先对虚拟节点.next虚拟节点.next.next.next临时记录
  • 如果为偶数个节点,则cur.next==None
  • 如果为奇数个节点,则cur.next.next==None
python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        virtualHead = ListNode()
        virtualHead.next = head
        cur = virtualHead
        while cur.next != None and cur.next.next != None:  # 这里得先判断cur.next != None,不然如果cur.next为None的话,那么cur.next.next就空指针溢出了
            temp1 = cur.next
            temp2 = cur.next.next.next
            cur.next = cur.next.next
            cur.next.next = temp1
            cur.next.next.next = temp2
            cur = cur.next.next
        return virtualHead.next

删除链表的倒数第N个节点

思路

  • 删除第N个节点,那么我们当前遍历的指针一定要指向第N个节点前一个节点
  • 使用虚拟头结点left,right双指针,初始时,两个指针都指向虚拟头节点
  • right指针先向后移动N次,之后,双指针一起向后移动,当rightNone时,left刚好指向倒数第N个节点
  • 所以right指针一开始向后移动N+1次,这样left就可以指向被删除节点前一个节点
python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        virtualHead = ListNode()
        virtualHead.next = head
        left = right = virtualHead
        while n+1>0:
            right = right.next
            n -= 1
        while right != None:
            right = right.next
            left = left.next
        left.next = left.next.next
        return virtualHead.next

链表相交

思路1

  • 先遍历出AB的各个长度lengthAlengthB
  • 计算出差值temp
  • 最长的从头开始向后遍历temp次
  • 然后curAcurB进行判断,如果相等则返回该节点,如果遍历完了还不相等则返回None
python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        curA, curB = headA, headB
        lengthA = lengthB = 0
        while curA:
            lengthA += 1
            curA = curA.next
        while curB:
            lengthB += 1
            curB = curB.next
        if lengthA > lengthB:
            temp = lengthA - lengthB
            curA, curB = headA, headB
            while temp:
                curA = curA.next
                temp -= 1
            while curA:
                if curA == curB:
                    return curA 
                curA = curA.next
                curB = curB.next
            return None
        else:
            temp = lengthB - lengthA
            curA, curB = headA, headB
            while temp:
                curB = curB.next
                temp -= 1
            while curA:
                if curA == curB:
                    return curA 
                curA = curA.next
                curB = curB.next
            return None

思路2

  • 使用集合记录一条链表所有内存地址哈希表
  • 再去遍历另一条链表每遍历一个节点,就判断该节点内存地址在不在集合中
  • 如果就返回该节点不在则返回None
python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        curA, curB = headA, headB
        hash1 = set()
        while curA:
            hash1.add(curA)
            curA = curA.next
        while curB:
            if curB in hash1:
                return curB
            curB = curB.next
        return None

环形链表II

思路

  • 使用left,right快慢指针两个指针第一次相遇时一定在环中相遇前left每次移动一个节点right每次移动两个节点
  • left,right再次相遇时,就是入环口
python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        left = right = head
        while right and right.next:  # right不为None,right.next也不为None
            left = left.next
            right = right.next.next
            if left == right:
                left = head
                while left != right:
                    left = left.next
                    right = right.next
                return left
        return None
相关推荐
姜不吃葱13 分钟前
【力扣热题100】哈希——两数之和
算法·leetcode·哈希算法·力扣热题100
普郎特20 分钟前
大白话帮你彻底理解 aiohttp 的 ClientSession 与 ClientResponse 对象
爬虫·python
AI4Sci.22 分钟前
在云服务器上基于lora微调Qwen2.5-VL-7b-Instruct模型(下)
人工智能·算法·机器学习·大模型·lora微调·大模型本地部署·qwen2.5-vl-7b
一只小风华~24 分钟前
JavaScript:数组常用操作方法的总结表格
前端·javascript·数据结构·vue.js·算法
TiAmo zhang1 小时前
深度学习与图像处理 | 基于PaddlePaddle的梯度下降算法实现(线性回归投资预测)
图像处理·深度学习·算法
空中湖1 小时前
PyTorch武侠演义 第一卷:初入江湖 第7章:矿洞中的计算禁制
人工智能·pytorch·python
一匹电信狗1 小时前
【C++】手搓一个STL风格的vector容器
c语言·数据结构·c++·算法·leetcode·stl·visual studio
Emma歌小白1 小时前
**大数据量(几千万行)划分价格区间(价格段)
python
生信探索1 小时前
SeuratExtend 可视化教程(1):单细胞分析的高颜值绘图指南
算法
小小小白的编程日记1 小时前
C语言中的数据结构--栈和队列(2)
c语言·数据结构