当前位置:网站首页>剑指 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;
}
小结
对于链表的操作,要考虑访问数据是不同于顺序表的,所以思路会有一定的差异。我们需大量的去刷题,锻炼编程思维, 相信坚持下去会有不一样的收获🤨。
边栏推荐
- CPDA | how to start the growth path of data analysts?
- shell小技巧(一百三十四)简单的键盘输入记录器
- China Mobile MCU product information
- STL tutorial 4- input / output stream and object serialization
- 差点被这波Handler 面试连环炮带走~
- Sichuan earth microelectronics 8-channel isolated digital input receiver
- OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
- Modular programming of oled12864 display controlled by single chip microcomputer
- Five causes of PCB board deformation and six solutions 2021-10-08
- PI Ziheng embedded: This paper introduces the multi-channel link mode of i.mxrt timer pit and its application in coremark Test Engineering
猜你喜欢
[batch dos-cmd command - summary and summary] - application startup and call, service and process operation commands (start, call, and)
【批处理DOS-CMD命令-汇总和小结】-添加注释命令(rem或::)
机器学习笔记 - 时间序列的线性回归
[batch dos-cmd command - summary and summary] - file and directory operation commands (MD, RD, xcopy, dir, CD, set, move, copy, del, type, sort)
函数模板_类模板
Tupu software digital twin 3D wind farm, offshore wind power of smart wind power
Full range of isolator chips with integrated isolated power supply
Home environment monitoring system design (PC version) (mobile app version to be determined)
(tool class) use SecureCRT as the communication medium
el-input实现尾部加字
随机推荐
lebel只想前面有星号,但是不想校验
Evolution of Alibaba e-commerce architecture
Chuantu microelectronics 𞓜 subminiature package isolated half duplex 485 transceiver
opencv最小值滤波(不局限于图像)
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
(tool class) quickly add time to code in source insight
【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令(cd、title、mode、color、pause、chcp、exit)
Can I open a stock account with a compass? Is it safe?
VectorDraw Web Library 10.10
Pytorch遇到的坑:为什么模型训练时,L1loss损失无法下降?
NSIS 静默安装vs2013运行时
【QT】Qt 5 的程序:打印文档
Static bit rate (CBR) and dynamic bit rate (VBR)
[pytest] modify the logo and parameterization in the allure Report
Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer
机器学习笔记 - 时间序列的线性回归
WinForm implementation window is always at the top level
China Mobile MCU product information
【Qt】快捷键
Home environment monitoring system design (PC version) (mobile app version to be determined)