当前位置:网站首页>[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;
}
边栏推荐
- 李沐动手学深度学习V2-BERT预训练和代码实现
- 拥抱Cmake小朋友 简单又实用,但是不灵活
- Common tools and test methods for interface testing (Introduction)
- Informatics Olympiad All-in-One (1257: Knight Moves)
- 10 种最佳 IDE 软件 ,你更忠爱哪一个?
- Details in C# you don't know
- vscode如何能将输出从OUTPUT改为TERMINAL或者DebugConsole
- 交 叉 数 组
- 汇编语言中b和bl关键字的区别
- OP-5,输入/输出信号范围-一信号处理能力
猜你喜欢

Electrical diagram of power supply system

Axure9的元件用法

包管理工具Chocolate - Win10如何安装Chocolate工具、快速上手、进阶用法

vscode如何能将输出从OUTPUT改为TERMINAL或者DebugConsole

李沐动手学深度学习V2-BERT预训练和代码实现

.NET性能优化-你应该为集合类型设置初始大小

你是几星测试/开发程序员?技术型选手王大拿......

KDD 2022 | 深度图神经网络中的特征过相关:一个新视角

WPF development through practical 】 【 automatic production management platform

千人优学 | GBase 8s数据库2022年6月大学生专场实训圆满结束
随机推荐
华为设备配置BFD多跳检测
Xcode13.1 run engineering error fatal error: 'IFlyMSC/IFly h' file not found
Jar包启动通过ClassPathResource获取不到文件路径问题
HCIP--路由策略实验
"Weekly Translate Go" This time we have something different!-- "How to Code in Go" series launched
用户之声 | GBASE南大通用实训有感
OP-5,输入/输出信号范围-一信号处理能力
浅议.NET遗留应用改造
56.【全局变量和局部变量专题】
包管理工具npm- node package management相关知识 、检查包更新、NPM包上传、更换镜像、npm ERR! registry error parsing json
如何理解 swing 是非线程安全 (原创)
力扣每日一题-第46天-344. 反转字符串
setup syntax sugar defineProps defineEmits defineExpose
千人优学 | GBase 8s数据库2022年6月大学生专场实训圆满结束
KDD 2022 | 深度图神经网络中的特征过相关:一个新视角
Details in C# you don't know
y85.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶、pushgateway和prometheus存储(十六)
信息学奥赛一本通(1256:献给阿尔吉侬的花束)
快速构建电脑软件系统 、超好用经典的网页推荐汇总
The Orsay in Informatics (1256: Bouquet for Algernon)