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的更多相关文章

  1. [LeetCode] 234. Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  2. 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 ...

  3. 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 ...

  4. (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 ...

  5. 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) ...

  6. LeetCode 234 Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...

  7. [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 ...

  8. LeetCode(55)- Palindrome Linked List

    题目: Given a singly linked list, determine if it is a palindrome. Follow up: 思路: 题意:判断一个链表是不是回文 利用两个指 ...

  9. LeetCode 234 Palindrome Linked List(回文链表)(*)(?)

    翻译 给定一个单链表,确定它是否是回文的. 跟进: 你能够在O(n)时间和O(1)空间下完毕它吗? 原文 Given a singly linked list, determine if it is ...

随机推荐

  1. jquery ajax清除缓存的方法

    function cityListChange(cityCode){ //{lon=121.491121, name=上海, province=上海市, telPrefix=021, province ...

  2. POJ 1305 Fermat vs. Pythagoras (毕达哥拉斯三元组)

    设不定方程:x^2+y^2=z^2若正整数三元组(x,y,z)满足上述方程,则称为毕达哥拉斯三元组.若gcd(x,y,z)=1,则称为本原的毕达哥拉斯三元组. 定理:正整数x,y,z构成一个本原的毕达 ...

  3. HDU 2955 Robberies (01背包,思路要转换一下,推荐!)

    题意: 小A要去抢劫银行,但是抢银行是有风险的,因此给出一个float值P,当被抓的概率<=p,他妈妈才让他去冒险. 给出一个n,接下来n行,分别给出一个Mj和Pj,表示第j个银行所拥有的钱,以 ...

  4. 【leetcode】Multiply Strings(middle)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. Android 内存剖析 – 发现潜在问题

    简介 移动平台上的开发和内存管理紧密相关.尽管随着科技的进步,现今移动设备上的内存大小已经达到了低端桌面设备的水平,但是现今开发的应用程序对内存的需求也在同步增长.主要问题出在设备的屏幕尺寸上-分辨率 ...

  6. HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  7. poj 3537 Crosses and Crosses 博弈论

    思路:每次画X之后都会形成2个子游戏,即i-3和n-i-2. 代码如下: #include<iostream> #include<cstdio> #include<cma ...

  8. Java集合框架(一)

    Java中集合类的关系图:  Collection 先来集合中的最大接口——Collection 可以通过查看JDK帮助文档,了解Collection接口中的最共性的方法.通过以下代码示例演示这些方法 ...

  9. java重构、重载、重写

    重构:就是代码优化,或则你可以理解为代码的修改!            例:开始你的类名是A现在要改为B也称为重构的一种 重载:发生在同一类中,方法名相同,参数列表不同 重写:发生在父子类中,子类中有 ...

  10. 以Server模式启动Derby服务竟然抛套接字权限异常

    以Server模式启动Derby服务竟然抛套接字权限异常:access denied ("java.net.SocketPermission" "localhost:15 ...