代码随想录算法训练营第四天-链表part2

day2和day3回家太晚,刷完题忘记写笔记了 - -!

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

给自己的笔记:

虚拟节点法是创建一个节点,它的next指针指向链表的头节点,这样便于:

  1. current指向虚拟节点,然后对链表进行操作交换
  2. 最后返回头节点:return dummyNode.next
python 复制代码
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        #创建虚拟节点
        dummyNode=ListNode(next=head)
        #curr 指向dummyNode
        curr=dummyNode
        while(curr.next and curr.next.next):#cur的下一位和下下一位都在 才会进入循环
            temp=curr.next
            temp1=curr.next.next.next
            curr.next=curr.next.next
            curr.next.next=temp
            temp.next=temp1
            curr=curr.next.next#curr往前2位
        return dummyNode.next #返回头节点
       

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

  1. 笨办法,先遍历所有节点获得链表长度。
python 复制代码
class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        #虚拟头节点
        dummyNode=ListNode(next=head)
        curr=dummyNode
        sizeLink=0
        count=0
        while(curr.next):
            sizeLink+=1
            curr=curr.next
        x=sizeLink-n
        curr=dummyNode
        if(sizeLink==1):
            return None
        if(sizeLink==n):
            return head.next
        while(curr.next):
            curr=curr.next
            count+=1
            if(count==x):
                curr.next=curr.next.next   
        return dummyNode.next
  1. 双指针法
    tips:数组也是可以这样的思路
python 复制代码
class Solution(object):
    def removeNthFromEnd(self, head, n):
        dummyNode=ListNode(next=head)
        slow=dummyNode
        fast=slow
        curr=dummyNode
        for i in range(n+1):#注意是n+1,这样快指针走到尾,慢指针停在要删的节点前一个节点
            fast=fast.next
        
        while(fast!=None):
            fast=fast.next
            slow=slow.next
        slow.next=slow.next.next
        return dummyNode.next

面试题 02.07. 链表相交

核心思想是求两个链表长度差,让指向长链表头指针移动到,和短链表头指针对齐的位置

比如:

A: 4,1,8,4,5

currA->4

B:5,0,1,8,4,5

currB->0

python 复制代码
class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        dummyNodeA=ListNode(next=headA)
        currA=dummyNodeA
        sizeA,sizeB=0,0
        dummyNodeB=ListNode(next=headB)
        currB=dummyNodeB
        curr,curr2=headA,headB
        while curr:
            curr=curr.next
            sizeA+=1
        while curr2:
            curr2=curr2.next
            sizeB+=1
        diff=abs(sizeA-sizeB)#长度差
        
        for i in range(diff):#让长链表指针移动到,和短链表头指针对齐的位置
            if sizeA>sizeB:
                currA=currA.next
            else:
                currB=currB.next
        
        while currA=currB #当指针相等
            currA=currA.next
            currB=currB.next
        return currA
        
        

142.环形链表II

...待补充

相关推荐
蓝斯49729 分钟前
Diff算法的简单介绍
算法
过期的秋刀鱼!33 分钟前
L2正则化,防止过拟合-历史补充
算法·过拟合·l2正则化
hansang_IR1 小时前
【题解】LC:Z 算法(Z Algorithm)
c++·算法·字符串
范什么特西1 小时前
回答知识总结02
算法·哈希算法
橘子汽水1682 小时前
Leetcode 230,98:二叉搜索树中第K小的元素,验证二叉搜索树
算法·leetcode·职场和发展
m沐沐2 小时前
【机器学习】DBSCAN聚类算法——原理、参数调优与实战
人工智能·python·深度学习·算法·机器学习·聚类·dbscan
问商十三载2 小时前
GEO优化的6个核心诊断工具,2026年实操版详解
人工智能·算法·机器学习
手写码匠2 小时前
华为云征文|DeepSeek-R1 智能问数 Agent 实战:Flexus X 实例 + Dify 构建企业级 Text-to-SQL 查询助手
人工智能·深度学习·算法·aigc
mifengxing3 小时前
Java集合与泛型
java·算法·复习笔记
liulilittle3 小时前
[OPENPPP2] ssea 算法详解(中文)
算法·x86·x64·simd·openppp2·ssea·base94