当前位置:网站首页>[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;
}
边栏推荐
- 五大维度解读软件测试分类
- 汉源高科千兆4光4电工业级网管型智能环网冗余以太网交换机防浪涌防雷导轨式安装
- 解道6-编程技术3
- 交 叉 数 组
- Informatics orsay a tong (1258: 【 9.2 】 digital pyramid)
- X 2 Earn必须依靠旁氏启动?GameFi的出路在哪?(下)
- 开关、电机、断路器、电热偶、电表接线图大全
- Tencent YunMeng every jie: I experienced by cloud native authors efficiency best practices case
- 李沐动手学深度学习V2-bert和代码实现
- 封装和包、访问修饰权限
猜你喜欢

《分布式微服务电商》专题(一)-项目简介

李沐动手学深度学习V2-bert和代码实现

汉源高科千兆4光4电工业级网管型智能环网冗余以太网交换机防浪涌防雷导轨式安装

奥特学园ROS笔记--7(289-325节)

Flink Yarn Per Job - 启动AM

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

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

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

ICLR 2022最佳论文:基于对比消歧的偏标签学习

Li Mu hands-on learning deep learning V2-bert and code implementation
随机推荐
pytorch的tensor创建和操作记录
How to use windbg check c # a thread stack size?
js how to get the browser zoom ratio
【3D视觉】realsense D435三维重建
"A daily practice, happy water problem" 1374. Generate a string with an odd number of each character
Flink Yarn Per Job - 创建启动Dispatcher RM JobManager
Implement fashion_minst clothing image classification
.NET performance optimization - you should set initial size for collection types
模糊查询like用法实例(Bee)
iframe------------frame-
MSTP与STP
arm64麒麟安装paddlehub(国产化)
回文自动机+CodeTON Round 2 C,D
Golang source code analysis: juju/ratelimit
奥特学园ROS笔记--7(289-325节)
用户之声 | GBASE南大通用实训有感
Solve the docker mysql can't write Chinese
包管理工具Chocolate - Win10如何安装Chocolate工具、快速上手、进阶用法
OP-5,输入/输出信号范围-一信号处理能力
软件测试的流程规范有哪些?具体要怎么做?