当前位置:网站首页>[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;
}
边栏推荐
猜你喜欢

【流媒体】推流与拉流简介

go——内存分配机制
![Informatics Olympiad All-in-One (1259: [Example 9.3] Find the longest non-descending sequence)](/img/a2/6d548909341a65129db2e69b90e5bf.png)
Informatics Olympiad All-in-One (1259: [Example 9.3] Find the longest non-descending sequence)

Common tools and test methods for interface testing (Introduction)

Wiring diagrams of switches, motors, circuit breakers, thermocouples, and meters

用户之声 | 我与GBase的缘分

信息学奥赛一本通(1258:【例9.2】数字金字塔)

交 叉 数 组

HCIP--路由策略实验

Flink Yarn Per Job - 启动AM
随机推荐
JMeter的基本使用
特拉维夫大学 | Efficient Long-Text Understanding with Short-Text Models(使用短文本模型进行高效的长文本理解)
引用类型 ,值类型 ,小坑。
Day12 接口和协议
接口测试常用工具及测试方法(入门篇)
How to quickly compare two byte arrays for equality in .NET
Golang source code analysis: time/rate
2170. 使数组变成交替数组的最少操作数
基本语法(三)
"A daily practice, happy water problem" 1374. Generate a string with an odd number of each character
Bee 事务注解 @Tran 使用实例
WPF development through practical 】 【 automatic production management platform
Flutter 常见异常分析
14、学习MySQL 连接的使用
供电系统电气图
X 2 Earn必须依靠旁氏启动?GameFi的出路在哪?(下)
【3D视觉】深度摄像头与3D重建
Flink Yarn Per Job - 启动AM
【SLAM】DM-VIO(ros版)安装和论文解读
C语言中变量在内存中的保存与访问