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之中,也可以遍历链表然后将list中的值都压倒堆栈中再和List进行比较,

但是想要达到题目上面的 时间复杂度为O(n),而空间复杂度为O(1)还要用到递归:

首先是不用递归的方法(使用一个栈):

 /**
* 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) {
ListNode * tmpHead = head;
stack<int> lt;
while (tmpHead){
lt.push(tmpHead->val);
tmpHead = tmpHead->next;
} while (!lt.empty()){
if (head->val != lt.top()) return false;
lt.pop();
head = head->next;
}
return true;
}
};

java版本的代码:

 public class Solution {
public boolean isPalindrome(ListNode head) {
ListNode p = head;
Stack<ListNode> s = new Stack<ListNode>();
while (p != null) {
s.push(p);
p = p.next;
}
while(!s.isEmpty()){
if(head.val != s.pop().val)
return false;
head = head.next;
}
return true;
}
}

首先上面这个空间复杂度是不满足要求的,再者,既然想到了将元素压入栈中,那么结合递归的话,应该就能做到空间复杂度O(1)了,应为上面的方法用到了堆栈,与递归能很好的结合:

 class Solution{
private:
ListNode * theHead; public:
bool isPalindrome(ListNode* head) {
theHead = head;  //先将head保存到一个theHead里面,便于与后面的递归了得指针相比较。
return valid(head);
} bool valid(ListNode * first)
{
if (first == NULL) return true;
if (valid(first->next) == false) return false;
if (first->val == theHead->val){
theHead = theHead->next;
return true;
}
else
return false;
}
};

因为道理比较简单,所以就不一一赘述了。

java版本的代码如下所示:

 public class Solution {
ListNode helper;
public boolean isPalindrome(ListNode head) {
helper = head;
return isValid(head);
} public boolean isValid(ListNode first){
if(first == null) return true;
if(isValid(first.next) == false) return false;
if(first.val == helper.val){
helper = helper.next;
return true;
}else
return false;
}
}

LeetCode OJ: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. [CareerCup] 2.7 Palindrome Linked List 回文链表

    2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...

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

  4. 234 Palindrome Linked List 回文链表

    请检查一个链表是否为回文链表. 进阶:你能在 O(n) 的时间和 O(1) 的额外空间中做到吗? 详见:https://leetcode.com/problems/palindrome-linked- ...

  5. lintcode 中等题:Palindrome Linked List 回文链表

    题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的 ...

  6. LeetCode OJ Palindrome Number(回文数)

    class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...

  7. LeetCode 9 Palindrome Number(回文数字判断)

    Long Time No See !   题目链接https://leetcode.com/problems/palindrome-number/?tab=Description   首先确定该数字的 ...

  8. leetcode面试题 02.06. 回文链表,解题心路

    目录 leetcode面试题 02.06. 回文链表,解题心路 1.题目描述 2.java语言题解一 3.java语言题解二 4.C语言题解一 leetcode面试题 02.06. 回文链表,解题心路 ...

  9. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

随机推荐

  1. 在Web上运行Linux—js/linux模拟器

    一个叫Fabrice Bellard 的程序员写了一段Javascript在Web浏览器中启动Linux(原网页,我把这个网页iframe在了下面),目前,你只能使用Firefox 4和Chrome ...

  2. node.js---sails项目开发(4)---配置MongoDB数据库连接

    1.安装sails对mongo的依赖 npm install sails-mongo --save 2. 配置mongo连接 修改config/connections.js: module.expor ...

  3. Delphi 正则表达式语法(3): 匹配范围

    Delphi 正则表达式语法(3): 匹配范围 // [A-Z]: 匹配所有大写字母 var   reg: TPerlRegEx; begin   reg := TPerlRegEx.Create(n ...

  4. CSS 中文字体 Unicode 编码表

    CSS 中文字体 Unicode 编码表 在 CSS 中设置字体名称,直接写中文是可以的.但是在文件编码(GB2312.UTF-8 等)不匹配时会产生乱码的错误. 为此,在 CSS 直接使用 Unic ...

  5. 剑指offer编程题66道题 26-35

    26.二叉搜索树与双向链表 题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向.   中序遍历思路:按照右中左的顺序,中序遍历对 ...

  6. vue切换路由模式{hash/history}

    vue中常用的路由模式 hash(#):默认路由模式 histroy(/)切换路由模式 切换路由模式 export default new Router({ // 路由模式:hash(默认),hist ...

  7. 在winform中,禁止combobox随着鼠标一起滑动!

    在winform中,如果form上或者是控件上有一个combobox控件,当你选择这个控件,当你鼠标移动其他地方,滑动鼠标时,这时combobox的选择值就会随之鼠标一起变化,如果你不想让comboB ...

  8. [BZOJ1116]CLO[并查集]

    看了样例突然发现= =无向边不会增加入度. 然后发现是环套环. 一个环所有点入度都为2. 最后的图无视所有无向边的话大概是这样的(将就一下 然后就可以并查集维护一下联通性... 当x , y属于一个联 ...

  9. MyEclipse 为xml添加本地的dtd文件

    在使用Eclipse或MyEclipse编辑XML文件的时候经常会碰到编辑器不提示的现象,这常常是因为其xml文件需要参考的DTD文件找不到,还有因为网络的问题不能及时提示而产生的.Eclipse/M ...

  10. Minimum Path Sum,最短路径问题,动态规划

    问题描述:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...