[LeetCode]Palindrome Linked List
来源:互联网 发布:linux x86 x64区别 编辑:程序博客网 时间:2024/06/03 07:29
题目:判断一个单链表是不是回文, O(n) time and O(1) space
分析:使用龟兔法找到链表的中心,反转后半段链表,然后与前半段比较
参考代码:
bool isPalindrome(struct ListNode* head) {
struct ListNode* fast,*slow,*pre,*cur,*ne;
fast=slow=head;
if(!head||!head->next)
{
return true;
}
while(fast&&fast->next)
{
slow=slow->next;
fast=fast->next->next;
}
if(fast)
{
slow=slow->next;
pre=slow;
cur=pre->next;
while(cur)
{
ne=cur->next;
cur->next=pre;
pre=cur;
cur=ne;
}
slow->next=NULL;
slow=pre;
}else{
pre=slow;
cur=pre->next;
while(cur)
{
ne=cur->next;
cur->next=pre;
pre=cur;
cur=ne;
}
slow->next=NULL;
slow=pre;
}
cur=head;
while(slow&&cur)
{
if(cur->val!=slow->val)
return false;
else {
cur=cur->next;
slow=slow->next;
}
}
return true;
}
- LeetCode Palindrome Linked List
- LeetCode - Palindrome Linked List
- leetcode: Palindrome Linked List
- Leetcode: Palindrome Linked List
- leetcode:Palindrome Linked List
- [LeetCode] Palindrome Linked List
- LeetCode || Palindrome Linked List
- 【leetcode】Palindrome Linked List
- LeetCode-Palindrome Linked List
- Leetcode|Palindrome Linked List
- [leetcode] Palindrome Linked List
- [LeetCode]Palindrome Linked List
- [Leetcode]Palindrome Linked List
- [leetcode] Palindrome Linked List
- [leetCode] Palindrome Linked List
- LeetCode----Palindrome Linked List
- LeetCode:Palindrome Linked List
- LeetCode Palindrome Linked List
- hiho一下 第五十八周
- 细说ReactiveCocoa的冷信号与热信号(二):为什么要区分冷热信号
- 判断字符是否出现
- 约束及修改数据表
- pip安装django失败
- [LeetCode]Palindrome Linked List
- Linux命令备忘实例(14)——sed
- 对连接数据库的简单升级
- 从1到n整数中1出现的次数(有疑问)
- 【UIKit-110-7】#import <UIKit/UITableView.h>提前注册
- Xcode 6.3.1Mac版 V6.4.Beta3免费下载
- yum提示another app is currently holding the yum lock;waiting for it to exit
- C语言中可变长参数使用
- 韩国美女连连看核心算法