当前位置:网站首页>[C题目]力扣234. 回文链表
[C题目]力扣234. 回文链表
2022-08-02 20:33:00 【GLC8866】
思路:
struct ListNode* MiddleNode(struct ListNode* head)
{
struct ListNode* slow=head;
struct ListNode* fast=head;
while(fast&&fast->next)//防止结点为奇数个时,fast->next先为NULL,fast应该写在fast->next的左边。
{
fast=fast->next->next;
slow=slow->next;
}
return slow;
}
struct ListNode* reverseList(struct ListNode* head)
{
//创建哨兵位的头结点
struct ListNode* newhead=(struct ListNode*)malloc(sizeof(struct ListNode));
newhead->next=NULL;//看作指向NULL结点
struct ListNode* cur=head;
while(cur)
{
struct ListNode* tmp=cur->next;
cur->next=newhead->next;
newhead->next=cur;
cur=tmp;
}
return newhead->next;
}
bool isPalindrome(struct ListNode* head)
{
struct ListNode* middle=MiddleNode(head);
struct ListNode* newhead=reverseList(middle);
struct ListNode* cur1=head;
struct ListNode* cur2=newhead;
while(cur2)//奇数个结点时,虽然后半段节点数比前半段结点数多一个,但当cur2指向middle时,cur1也存着middle的地址(指针域并没有被覆盖),所以cur2->val依然等于cur1->val,不影响判断两个链表是否相等。
{
if(cur2->val!=cur1->val)
return false;
cur1=cur1->next;
cur2=cur2->next;
}
return true;
}
边栏推荐
- 封装和包、访问修饰权限
- js: 实现一个cached缓存函数计算结果
- VisualStudio 制作Dynamic Link Library动态链接库文件
- 开关、电机、断路器、电热偶、电表接线图大全
- 【3D视觉】realsense D435三维重建
- Qt提升自定义控件,找不到头文件
- 华为设备配置BFD多跳检测
- y85.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶、pushgateway和prometheus存储(十六)
- Nervegrowold hands-on learning deep learning V2 - Bert pre training data set and code implementation
- Bena's life cycle
猜你喜欢

华为设备配置BFD多跳检测

李沐动手学深度学习V2-bert和代码实现
Adobe官方清理工具Adobe Creative Cloud Cleaner Tool使用教程

「每周译Go」这次我们来点不一样的!--《How to Code in Go》系列上线

A brief discussion on the transformation of .NET legacy applications

Day12 接口和协议

特拉维夫大学 | Efficient Long-Text Understanding with Short-Text Models(使用短文本模型进行高效的长文本理解)

.NET如何快速比较两个byte数组是否相等

The software testing process specification is what?Specific what to do?

美国爱荷华州立大学| Improving Distantly Supervised Relation Extraction by Natural Language Inference(通过自然语言推理改进远程监督关系提取)
随机推荐
性能测试 - 理论
HCIP--路由策略实验
开关、电机、断路器、电热偶、电表接线图大全
Xcode13.1运行工程报错fatal error: ‘IFlyMSC/IFly.h‘ file not found的问题
56.【全局变量和局部变量专题】
"Weekly Translate Go" This time we have something different!-- "How to Code in Go" series launched
Electrical diagram of power supply system
快速构建电脑软件系统 、超好用经典的网页推荐汇总
js: 实现一个cached缓存函数计算结果
浅议.NET遗留应用改造
信息学奥赛一本通(1259:【例9.3】求最长不下降序列)
PLC工作原理动画
信息学奥赛一本通(1257:Knight Moves)
Day35 LeetCode
Li Mu hands-on deep learning V2-BERT pre-training and code implementation
如何成为一名正义黑客?你应该学习什么?
用了TCP协议,就一定不会丢包吗?
DataGrip 安装教程 详细版
Geoip2 - golang golang source code analysis
ORB SLAM3加载Vocabulary更快ORBvoc.bin