【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表

《博主简介》

小伙伴们好,我是阿旭。专注于人工智能AI、python、计算机视觉相关分享研究。

更多学习资源,可关注公-仲-hao:【阿旭算法与机器学习】,共同学习交流~

👍感谢小伙伴 们点赞、关注!

快慢指针

移动零

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| class Solution : def moveZeroes****(**** self****,**** nums****:**** List****[**** int ]) -> None : """ Do not return anything, modify nums in-place instead. """ left = 0 n = len ( nums****)**** for i in range ( n****):**** if nums****[**** i****]**** != 0****:**** nums****[**** left****],**** nums****[**** i****]**** = nums****[**** i****],**** nums****[**** left****]**** left += 1 return nums class Solution : def moveZeroes****(**** self****,**** nums****:**** List****[**** int ]) -> None : """ Do not return anything, modify nums in-place instead. """ j = 0 for i in range ( len ( nums****)):**** if nums****[**** i****]**** != 0****:**** nums****[**** j****]**** = nums****[**** i****]**** if i != j****:**** nums****[**** i****]**** = 0 j += 1 return nums |

链表

两两交换链表中的节点

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| # 迭代 class Solution : def swapPairs****(**** self****,**** head****:**** ListNode****)**** -> ListNode****:**** # 通过迭代实现 dummy = ListNode****(-**** 1****)**** dummy****.**** next = head prev_node = dummy while head and head****.**** next : first_node = head second_node = head****.**** next # 交换节点 prev_node****.**** next = second_node first_node****.**** next = second_node****.**** next second_node****.**** next = first_node # 初始化头节点与prev_node prev_node = first_node head = first_node****.**** next return dummy****.**** next # 递归 class Solution : def swapPairs****(**** self****,**** head****:**** ListNode****)**** -> ListNode****:**** # 递归实现 if not head or not head****.**** next : return head first_node = head second_node = head****.**** next # 第二个节点的next节点作为头部传入递归函数,返回的是 # 指向第二个节点的指针 first_node****.**** next = self****.**** swapPairs****(**** second_node****.**** next ) second_node****.**** next = first_node return second_node |

反转链表

将链表进行反转

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| # 迭代 class Solution : def reverseList**(** self**,** head**:** ListNode**)** -> ListNode**:** if head is None : return head pre = None cur = head while cur**:** nxt = cur**.** next cur**.** next = pre pre = cur cur = nxt return pre # 递归 class Solution : def reverseList**(** self**,** head**:** ListNode**)** -> ListNode**:** if not head or not head**.** next : return head last = self**.** reverseList**(** head**.** next ) head**.** next . next = head head**.** next = None return last |

关于本篇文章大家有任何建议或意见,欢迎在评论区留言交流!

觉得不错的小伙伴,感谢点赞、关注加收藏哦!
欢迎关注下方GZH:阿旭算法与机器学习,共同学习交流~

相关推荐
luckys.one10 小时前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
~|Bernard|12 小时前
在 PyCharm 里怎么“点鼠标”完成指令同样的运行操作
算法·conda
战术摸鱼大师12 小时前
电机控制(四)-级联PID控制器与参数整定(MATLAB&Simulink)
算法·matlab·运动控制·电机控制
Christo312 小时前
TFS-2018《On the convergence of the sparse possibilistic c-means algorithm》
人工智能·算法·机器学习·数据挖掘
好家伙VCC13 小时前
数学建模模型 全网最全 数学建模常见算法汇总 含代码分析讲解
大数据·嵌入式硬件·算法·数学建模
liulilittle14 小时前
IP校验和算法:从网络协议到SIMD深度优化
网络·c++·网络协议·tcp/ip·算法·ip·通信
bkspiderx16 小时前
C++经典的数据结构与算法之经典算法思想:贪心算法(Greedy)
数据结构·c++·算法·贪心算法
中华小当家呐17 小时前
算法之常见八大排序
数据结构·算法·排序算法
沐怡旸17 小时前
【算法--链表】114.二叉树展开为链表--通俗讲解
算法·面试
tju新生代魔迷18 小时前
数据结构:双向链表
数据结构·链表