当前位置:网站首页>Sword finger offer II 027 Palindrome linked list
Sword finger offer II 027 Palindrome linked list
2022-06-25 07:52:00 【Little astronaut w】
In daily question brushing
GitHub link
diwei00 (github.com)
https://github.com/diwei00 All the codes are in Exercise Submit in Library
LeetCode link
subject
Given a linked list Head node
head, Please judge whether it is a palindrome linked list .If a linked list is palindrome , Then the list node sequence is the same from front to back and from back to front .
Example 1
Input : head = [1,2,3,3,2,1] Output : trueExample 2
Input : head = [1,2] Output : false
title 🤨
First , The palindrome linked list is judged by comparing the data of the first node with the data of the last node , Then compare the data of the second node and the penultimate node , And so on . Because this is a single linked list , It doesn't have random access to data like a sequence table , You can only traverse the linked list from front to back .
Analyze the topic and find , Data comparison is to compare the first half and the second half of the linked list , So we need to find the middle node of the linked list . Because the comparison is made between the first half of the node data and the second half of the node data , So consider reversing the second half of the linked list .
Find the middle node of the linked list here , It uses the fast and slow pointer method , Go two steps at a time , Slow pointer one step at a time . Distinguish between even and odd node linked lists , When the fast pointer traverses the linked list , The slow pointer is just in the middle node of the linked list .
Here, reverse the linked list , The three pointer method is used , In order to find the next node after reversing .
The two ideas here are introduced in detail in this blog .
Code implementation ( There are detailed notes )
bool isPalindrome(struct ListNode* head)
{
struct ListNode* phead = head;
struct ListNode* fast = head;
struct ListNode* slow = head;
// The speed pointer finds the middle node
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;
// Reverse the second half of linked list
while (p2 != NULL)
{
p2->next = p1;
p1 = p2;
p2 = p3;
if (p3 != NULL)
{
p3 = p3->next;
}
}
// Comparison data
while (p1 != NULL)
{
if (p1->val == phead->val)
{
p1 = p1->next;
phead = phead->next;
}
else
{
return false;
}
}
// All data are equal
return true;
}Summary
For the operation of linked list , Consider that accessing data is different from sequential tables , So there will be some differences in thinking . We need to brush a lot of questions , Exercise programming thinking , I believe that persistence will lead to different results 🤨.
边栏推荐
- Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks
- The method of judging whether triode can amplify AC signal
- Terms and concepts related to authority and authentication system
- NSIS 静默安装vs2013运行时
- test
- Analysis and utilization of Microsoft Office Word remote command execution vulnerability (cve-2022-30190)
- Manufacturing process of PCB 2021-10-11
- Tips 𞓜 how to clean PCB boards 2021-10-22
- el-input实现尾部加字
- 饮食干预减轻癌症治疗相关症状和毒性
猜你喜欢

用函数的递归来解决几道有趣的题

How to use printf of 51 single chip microcomputer

饮食干预减轻癌症治疗相关症状和毒性

How to resize an image in C #

One "stone" and two "birds", PCA can effectively improve the dilemma of missing some ground points under the airborne lidar forest

搞清信息化是什么,让企业转型升级走上正确的道路

Fairmot yolov5s to onnx

Keil and Proteus joint commissioning

What are the problems with traditional IO? Why is zero copy introduced?

OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
随机推荐
27. 移除元素
@Resource和@Autowired注解的不同,为什么推荐@Resource?
Static bit rate (CBR) and dynamic bit rate (VBR)
MySQL interview - the response of executing SQL is relatively slow, and the troubleshooting ideas.
【深度学习 轻量型backbone】2022 EdgeViTs CVPR
Estimation of dense forest volume based on LIDAR point cloud with few ground points
Four software 2021-10-14 suitable for beginners to draw PCB
新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍
How to use ad wiring for PCB design?
Knowledge sharing 𞓜 conventional laminated structure of six layer PCB
How much do you know about electronic components on PCB?
“空间转换”显著提升陡崖点云的地面点提取质量
机器学习笔记 - 时间序列的线性回归
navicat定时任务无效
判断用户是否是第一次进入某个页面
微信小程序开通客服消息功能开发
【视频】ffplay 使用mjpeg格式播放usb摄像头
c# winform panel自定义图片和文字
使用报文和波形记录分析仪RoyalScope的帧统计功能排查CAN总线偶发性故障
力扣78:子集

