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

相关推荐
散峰而望1 小时前
【算法练习】算法练习精选:陶陶摘苹果(基础+升级)、Music Notes、字串变换,你能AC几道?
数据结构·c++·算法·leetcode·贪心算法·github·动态规划
暗夜猎手-大魔王1 小时前
转载--Hermes Agent 04 | Agent 主循环:一次对话背后发生了什么
人工智能·python·算法
手写码匠1 小时前
华为云Flexus+DeepSeek征文|基于华为云Flexus X实例 + Dify + DeepSeek 构建企业级智能知识库问答系统实战
人工智能·深度学习·算法·aigc
吴可可1232 小时前
Win7上开发CAD2004自定义实体全解析
c++·算法
YXXY3132 小时前
二叉树中的深搜算法介绍
算法
zz34572981132 小时前
C语言中字符串常量存储位置
c语言·开发语言·算法·青少年编程
noipp2 小时前
推荐题目:洛谷 P16510 [GKS 2015 #C] gRanks
java·c语言·开发语言·c++·python·算法
菜菜的顾清寒2 小时前
力扣HOT100(50)动态规划-零钱兑换
算法·leetcode·动态规划
周末也要写八哥2 小时前
三分钟读懂:如何解决做题数量不足的问题?
算法
8Qi82 小时前
LeetCode 148. 排序链表 —— 解法二:自底向上归并(迭代,O(1) 空间)
数据结构·算法·leetcode·链表·归并·迭代