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. ToolStripButton样式

    public static class Extensions { public static void SetMouseDownStyle(this ToolStripButton button) { ...

  2. java web项目,java类中获得WEB-INF路径

    private static String getWebInfPath() { URL url = 当前类.class.getProtectionDomain().getCodeSource().ge ...

  3. wifi current SSID

    1. 引入头,#import <SystemConfiguration/CaptiveNetwork.h> 2. 获取SSID info + (id)fetchSSIDInfo { NSA ...

  4. Solr Schema.xml和solrconfig.xml分析(转)

    Solr Schema.xml和solrconfig.xml分析 (http://yinwufeng.iteye.com/blog/964040) 一.字段配置(schema) schema.xml位 ...

  5. UML类图(转载)

    概述: 类图是静态图.它代表了一个应用程序的静态视图.类图不仅用于可视化描述和记录系统的不同方面,但也为构建可执行代码的软件应用程序. 类图描述一类的属性和操作,也对系统的约束.被广泛应用于类图的建模 ...

  6. BZOJ 3155: Preprefix sum

    大意:给一个数组,先求出SUM[I],然后动态的求出1-I的SUM[I]的和, 这题得化公式: 树状数组维护两个和:SUM(A[I])(1<=I<=X); SUM(A[I]*(N-I+1) ...

  7. vc++ 获取当前用户名

    #include<afxwin.h> #include <stdio.h> int main(void) { char userName[MAX_PATH]; unsigned ...

  8. HDU 4022 Bombing(stl,map,multiset,iterater遍历)

    题目 参考了     1     2 #define _CRT_SECURE_NO_WARNINGS //用的是STL中的map 和 multiset 来做的,代码写起来比较简洁,也比较好容易理解. ...

  9. POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)

    Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...

  10. MYSQL 遭遇 THE TOTAL NUMBER OF LOCKS EXCEEDS THE LOCK TABLE SIZE

    今天进行MySql 一个表数据的清理,经过漫长等待后出现 The total number of locks exceeds the lock table size 提示.以为是table_cache ...