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

JMeter的基本使用

Bee 事务注解 @Tran 使用实例

软件测试的流程规范有哪些?具体要怎么做?

开关、电机、断路器、电热偶、电表接线图大全

pytorch的tensor创建和操作记录

数字孪生助力智慧城市可视化建设

"Weekly Translate Go" This time we have something different!-- "How to Code in Go" series launched

Flink Yarn Per Job - 创建启动Dispatcher RM JobManager
VisualStudio 制作Dynamic Link Library动态链接库文件

引用类型 ,值类型 ,小坑。
随机推荐
How to quickly compare two byte arrays for equality in .NET
DataGrip 安装教程 详细版
Common tools and test methods for interface testing (Introduction)
模糊查询like用法实例(Bee)
信息系统项目管理师必背核心考点(五十八)变更管理的主要角色
供电系统电气图
Tencent YunMeng every jie: I experienced by cloud native authors efficiency best practices case
【流媒体】推流与拉流简介
包管理工具Chocolate - Win10如何安装Chocolate工具、快速上手、进阶用法
PLC working principle animation
Bee 事务注解 @Tran 使用实例
"Weekly Translate Go" This time we have something different!-- "How to Code in Go" series launched
Bena的生命周期
如何理解 swing 是非线程安全 (原创)
MSTP与STP
拥抱Cmake小朋友 简单又实用,但是不灵活
Use the TCP protocol, we won't lost package?
信息学奥赛一本通(1258:【例9.2】数字金字塔)
信息学奥赛一本通(1259:【例9.3】求最长不下降序列)
奥特学园ROS笔记--7(289-325节)