当前位置:网站首页>剑指 Offer II 027. 回文链表
剑指 Offer II 027. 回文链表
2022-06-25 06:40:00 【小小太空人w】
日常刷题中
GitHub链接
diwei00 (github.com)
https://github.com/diwei00刷题代码都在Exercise库中提交
LeetCode链接
剑指 Offer II 027. 回文链表 - 力扣(LeetCode)
https://leetcode.cn/problems/aMhZSa/
题目
给定一个链表的 头节点
head,请判断其是否为回文链表。如果一个链表是回文,那么链表节点序列从前往后看和从后往前看是相同的。
示例1
输入: head = [1,2,3,3,2,1] 输出: true示例2
输入: head = [1,2] 输出: false
题目解析🤨
首先,回文链表的判断要用第一个节点数据和最后一个节点数据进行比对,然后在用第二个节点和倒数第二个节点数据比对,以此类推。因为这是单链表,他不像顺序表那样可以随机访问数据,只能从前往后遍历链表。
分析题目发现,数据的比对是链表的前半部分和后半部分进行比较,所以我们需找到链表的中间节点。由于进行比对是前半部分节点数据和后半部分节点数据,所以考虑反转后半部分链表。
这里找链表的中间节点,用的是快慢指针法,快指针一次走两步,慢指针一次走一步。区分奇偶个数节点链表,当快指针遍历完链表时,慢指针刚好在链表的中间节点。
这里反转链表,用的是三指针法,为的是反转完可以找到下一个节点。
这里的两个思路在这篇博客中有详细介绍。
代码实现(有详细注释)
bool isPalindrome(struct ListNode* head)
{
struct ListNode* phead = head;
struct ListNode* fast = head;
struct ListNode* slow = head;
//快慢指针找中间节点
while (fast != NULL && fast->next != NULL)
{
fast = fast->next->next;
slow = slow->next;
}
struct ListNode* p1 = NULL;
struct ListNode* p2 = slow;
struct ListNode* p3 = slow->next;
//反转后半部分链表
while (p2 != NULL)
{
p2->next = p1;
p1 = p2;
p2 = p3;
if (p3 != NULL)
{
p3 = p3->next;
}
}
//比对数据
while (p1 != NULL)
{
if (p1->val == phead->val)
{
p1 = p1->next;
phead = phead->next;
}
else
{
return false;
}
}
//全部数据都相等
return true;
}小结
对于链表的操作,要考虑访问数据是不同于顺序表的,所以思路会有一定的差异。我们需大量的去刷题,锻炼编程思维, 相信坚持下去会有不一样的收获🤨。
边栏推荐
- JDBC-DAO层实现
- FairMOT yolov5s转onnx
- OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
- Sichuan Tuwei ca-if1051 can transceiver has passed aec-q100 grade 1 certification
- 机器学习笔记 - 时间序列的线性回归
- VectorDraw Developer Framework 10.10
- useMemo模拟useCallback
- 栅格地图(occupancy grid map)构建
- 权限、认证系统相关名词概念
- Research on 3D model retrieval method based on two channel attention residual network - Zhou Jie - paper notes
猜你喜欢

Summary of small problems in smartbugs installation

Sichuan Tuwei ca-if1051 can transceiver has passed aec-q100 grade 1 certification

CPDA | how to start the growth path of data analysts?

【批处理DOS-CMD命令-汇总和小结】-文件与目录操作命令(md、rd、xcopy、dir、cd、set、move、copy、del、type、sort)

Construction of occupancy grid map

Research on 3D model retrieval method based on two channel attention residual network - Zhou Jie - paper notes

STL tutorial 4- input / output stream and object serialization

How comfortable it is to use Taijiquan to talk about distributed theory!

Full range of isolator chips with integrated isolated power supply

【批處理DOS-CMD命令-匯總和小結】-cmd擴展命令、擴展功能(cmd /e:on、cmd /e:off)
随机推荐
Elk + filebeat log parsing, log warehousing optimization, logstash filter configuration attribute
Find out what informatization is, and let enterprises embark on the right path of transformation and upgrading
Tupu software digital twin 3D wind farm, offshore wind power of smart wind power
Estimation of dense forest volume based on LIDAR point cloud with few ground points
STL tutorial 4- input / output stream and object serialization
Accès à la boîte aux lettres du nom de domaine Lead à l'étranger
realsense d455 semantic_ Slam implements semantic octree mapping
Lebel only wants an asterisk in front of it, but doesn't want to verify it
WinForm implementation window is always at the top level
国外LEAD域名邮箱获取途径
FairMOT yolov5s转onnx
Explain distributed raft with dynamic diagram
C#入门教程
WinForm实现窗口始终在顶层
Bicubic difference
Collection of common terms and meanings in forestry investigation based on lidar
OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
[batch dos-cmd command - summary and summary] - add comment command (REM or::)
Storage of Galileo broadcast ephemeris in rtklib-b33
Introduction to Sichuan Tuwei ca-is3082w isolated rs-485/rs-422 transceiver

