【经典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:阿旭算法与机器学习,共同学习交流~

相关推荐
limingade1 小时前
手机实时提取SIM卡打电话的信令和声音-新的篇章(一、可行的方案探讨)
物联网·算法·智能手机·数据分析·信息与通信
jiao000014 小时前
数据结构——队列
c语言·数据结构·算法
迷迭所归处5 小时前
C++ —— 关于vector
开发语言·c++·算法
leon6255 小时前
优化算法(一)—遗传算法(Genetic Algorithm)附MATLAB程序
开发语言·算法·matlab
CV工程师小林5 小时前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
Navigator_Z6 小时前
数据结构C //线性表(链表)ADT结构及相关函数
c语言·数据结构·算法·链表
Aic山鱼6 小时前
【如何高效学习数据结构:构建编程的坚实基石】
数据结构·学习·算法
天玑y6 小时前
算法设计与分析(背包问题
c++·经验分享·笔记·学习·算法·leetcode·蓝桥杯
sjsjs116 小时前
【数据结构-一维差分】力扣1893. 检查是否区域内所有整数都被覆盖
数据结构·算法·leetcode
redcocal6 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘