leetcode:Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.
Follow up:
Could you do it in O(n) time and O(1) space?
分析:题意为判断单链表是否为回文的。
思路:首先想到的是 遍历一次单链表,将其元素装入vector,然后进行第二次遍历比较来判断回文。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
vector<int> temp;
while(head){
temp.push_back(head->val);
head=head->next;
}
for(int i=0,j=temp.size()-1;i<j;i++,j--){
if(temp[i]!=temp[j]) return false;
}
return true;
}
};
可是这种方法:时间复杂度O(n),空间复杂度O(n);
为了使空间复杂度为O(1),可以不采用vector等,思路:找到单链表的中点,进行拆分,逆转后半个链表,然后对这两个链表同时顺序遍历一次进行判断。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(head == NULL || head->next == NULL)
return true;
ListNode* mid = getMid(head);
ListNode* head2 = reverse(mid);
while(head && head2)
{
if(head->val != head2->val)
return false;
head = head->next;
head2 = head2->next;
}
return true;
}
ListNode* getMid(ListNode* head)
{// at least two nodes
ListNode* slow = head;
ListNode* fast = head;
ListNode* preslow = NULL;
do
{
fast = fast->next;
if(fast)
{
fast = fast->next;
preslow = slow;
slow = slow->next;
}
}while(fast != NULL);
preslow->next = NULL;
return slow;
}
ListNode* reverse(ListNode* head)
{
if(head == NULL || head->next == NULL)
return head;
else if(head->next->next == NULL)
{// two nodes
ListNode* tail = head->next;
head->next = NULL;
tail->next = head;
return tail;
}
else
{
ListNode* pre = head;
ListNode* cur = pre->next;
pre->next = NULL; // set tail
ListNode* post = cur->next;
while(post)
{
cur->next = pre;
pre = cur;
cur = post;
post = post->next;
}
cur->next = pre;
return cur;
}
}
};
或者:
同样O(n) time and O(1) space c++, fast and slow pointer
class Solution {
public:
bool isPalindrome(ListNode* slow, ListNode* fast)
{
if (fast == nullptr) {
half = slow;
return true;
}
if (fast->next == nullptr) {
half = slow->next;
return true;
}
if (isPalindrome(slow->next, fast->next->next) && slow->val == half->val) {
half = half->next;
return true;
}
return false;
}
bool isPalindrome(ListNode* head) {
return isPalindrome(head, head);
}
ListNode* half;
};
leetcode:Palindrome Linked List的更多相关文章
- [LeetCode] 234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- LeetCode OJ:Palindrome Linked List(回文链表判断)
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- LeetCode 234. Palindrome Linked List (回文链表)
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- (easy)LeetCode 234.Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- Java [Leetcode 234]Palindrome Linked List
题目描述: Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) ...
- LeetCode 234 Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...
- [LeetCode] 234. Palindrome Linked List 解题思路
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- LeetCode(55)- Palindrome Linked List
题目: Given a singly linked list, determine if it is a palindrome. Follow up: 思路: 题意:判断一个链表是不是回文 利用两个指 ...
- LeetCode 234 Palindrome Linked List(回文链表)(*)(?)
翻译 给定一个单链表,确定它是否是回文的. 跟进: 你能够在O(n)时间和O(1)空间下完毕它吗? 原文 Given a singly linked list, determine if it is ...
随机推荐
- [工作积累] Android dynamic library & JNI_OnLoad
Bionic libc doesn't load dependencies for current .so file (diff from Windows or Linux) so a explici ...
- CocoaPods 使用手册
CocoaPods 使用手册 CocoaPods 使用手册 ...
- PowerDesigner(八)-面向对象模型(用例图,序列图,类图,生成Java源代码及Java源代码生成类图)(转)
面向对象模型 面向对象模型是利用UML(统一建模语言)的图形来描述系统结构的模型,它从不同角度实现系统的工作状态.这些图形有助于用户,管理人员,系统分析人员,开发人员,测试人员和其他人员之间进行信息交 ...
- Linux常用命令大全(转载)
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...
- [转]日期格式化(yyyy-MM-dd)中,为什么 M 多大写?
最近犯了个可傻逼的错误,格式化年月日的时候不小心将yyyy-MM-dd写成YYYY-MM-dd,导致格式化结果中年不正确. 看看知乎上的说法 问题: http://www.zhihu.com/ques ...
- javascript实现数据结构:串--定长顺序存储表示以及kmp算法实现
串(string)(或字符串)是由零个或多个字符组成的有限序列.串中字符的数目称为串的长度.零个字符的串称为空串(null string),它的长度为零. 串中任意个连续的字符组成的子序列称为该串的子 ...
- AssetBundle依赖关系
原地址:http://www.cnblogs.com/realtimepixels/p/3652086.html Unity AssetBundle Dependencies In the last ...
- delphi的socket通讯 多个客户端 (转)
ClientSocket组件为客户端组件.它是通信的请求方,也就是说,它是主动地与服务器端建立连接. ServerSocket组件为服务器端组件.它是通信的响应方,也就是说,它的动作是监听以及被动接受 ...
- POJ 3185
The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4088 Accepted: 1609 D ...
- Freebie: Material Design UI Kit
点这里 Following the guidelines laid out by Google, this free UI kit has been designed so that you can ...