当前位置:网站首页>[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;
}
边栏推荐
- Qt提升自定义控件,找不到头文件
- WPF development through practical 】 【 automatic production management platform
- 2022年金九银十,Android面试中高频必问的问题汇总
- 你所不知道的C#中的细节
- Likou Question of the Day - Day 46 - 344. Reverse Strings
- 训练双塔检索模型,可以不用query-doc样本了?明星机构联合发文
- 美国爱荷华州立大学| Improving Distantly Supervised Relation Extraction by Natural Language Inference(通过自然语言推理改进远程监督关系提取)
- JMeter的基本使用
- 如何成为一名正义黑客?你应该学习什么?
- Solve the docker mysql can't write Chinese
猜你喜欢
Common tools and test methods for interface testing (Introduction)
奥特学园ROS笔记--7(289-325节)
A brief discussion on the transformation of .NET legacy applications
Digital twins help visualize the construction of smart cities
特拉维夫大学 | Efficient Long-Text Understanding with Short-Text Models(使用短文本模型进行高效的长文本理解)
用户之声 | 我与GBase的缘分
用了TCP协议,就一定不会丢包吗?
The software testing process specification is what?Specific what to do?
Tencent YunMeng every jie: I experienced by cloud native authors efficiency best practices case
软件成分分析:华为云重磅发布开源软件治理服务
随机推荐
解道8-编程技术5
Async的线程池使用的哪个?
力扣每日一题-第46天-344. 反转字符串
【21天学习挑战赛】冒泡排序与插入排序
Day35 LeetCode
训练双塔检索模型,可以不用query-doc样本了?明星机构联合发文
《分布式微服务电商》专题(一)-项目简介
快速构建电脑软件系统 、超好用经典的网页推荐汇总
二叉搜索树的实现
EasyExcel dynamic parsing and save table columns
Digital twins help visualize the construction of smart cities
callback prototype __proto__
Jar包启动通过ClassPathResource获取不到文件路径问题
用户之声 | GBASE南大通用实训有感
DataGrip 安装教程 详细版
pytorch的tensor创建和操作记录
STP生成树协议
How to quickly compare two byte arrays for equality in .NET
.NET performance optimization - you should set initial size for collection types
接口测试常用工具及测试方法(入门篇)