当前位置:网站首页>[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;
}
边栏推荐
- vscode如何能将输出从OUTPUT改为TERMINAL或者DebugConsole
- 李沐动手学深度学习V2-BERT预训练和代码实现
- Mysql用户管理
- 第七章 噪声
- 回文自动机+CodeTON Round 2 C,D
- "Weekly Translate Go" This time we have something different!-- "How to Code in Go" series launched
- Bee 事务注解 @Tran 使用实例
- Golang source code analysis: juju/ratelimit
- 用户之声 | 我与GBase的缘分
- 奥特学园ROS笔记--7(289-325节)
猜你喜欢
随机推荐
软件测试的流程规范有哪些?具体要怎么做?
包管理工具Chocolate - Win10如何安装Chocolate工具、快速上手、进阶用法
OP-5,输入/输出信号范围-一信号处理能力
一次线上事故,我顿悟了异步的精髓
你是几星测试/开发程序员?技术型选手王大拿......
五大维度解读软件测试分类
软件成分分析:华为云重磅发布开源软件治理服务
Bena的生命周期
有效解决MySQL报错:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES)
Bee 事务注解 @Tran 使用实例
Xcode13.1 run engineering error fatal error: 'IFlyMSC/IFly h' file not found
C#异步和多线程
汉源高科千兆4光4电工业级网管型智能环网冗余以太网交换机防浪涌防雷导轨式安装
setup语法糖 defineProps defineEmits defineExpose
博客主页rrs代码
快速构建电脑软件系统 、超好用经典的网页推荐汇总
Flink Yarn Per Job - 启动AM
一款免费的容器安全 SaaS 平台使用记录
训练双塔检索模型,可以不用query-doc样本了?明星机构联合发文
go——垃圾回收机制(GC)




![Informatics Olympiad All-in-One (1259: [Example 9.3] Find the longest non-descending sequence)](/img/a2/6d548909341a65129db2e69b90e5bf.png)



