1 Star 0 Fork 0

唐梓迅/leetcode题解

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
剑指offerⅡ027 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
唐梓迅 提交于 2022-04-16 14:45 . add 剑指offerⅡ027.
struct ListNode* FirstListEnd(struct ListNode* head)
{
struct ListNode* fast = head;
struct ListNode* slow = head;
while(fast->next != NULL && fast->next->next != NULL)
{
fast = fast->next->next;
slow = slow->next;
}
return slow;
}
struct ListNode*reserve(struct ListNode* head)
{
struct ListNode* pre = NULL;
struct ListNode* cur = head;
while(cur != NULL)
{
struct ListNode* nextTemp = cur->next;
cur->next = pre;
pre = cur;
cur = nextTemp;
}
return pre;
}
bool isPalindrome(struct ListNode* head){
if (head == NULL) {
return true;
}
// 找到前半部分链表的尾节点并反转后半部分链表
struct ListNode* firstHalfEnd = FirstListEnd(head);
struct ListNode* secondHalfStart = reserve(firstHalfEnd->next);
// 判断是否回文
struct ListNode* p1 = head;
struct ListNode* p2 = secondHalfStart;
while ( p2 != NULL) {
if (p1->val != p2->val) {
return false;
}
p1 = p1->next;
p2 = p2->next;
}
// 还原链表并返回结果
firstHalfEnd->next = reserve(secondHalfStart);
return true;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Tang-CMer/leetcode-problem-solving.git
git@gitee.com:Tang-CMer/leetcode-problem-solving.git
Tang-CMer
leetcode-problem-solving
leetcode题解
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385