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. ansible判定文件或者文件夹是否存在

    ansible 的常用模块中没有判定当文件存在或者不存在时,执行某个执行 使用下面方法能简单判定某个文件是否存在 --- - name: judge a file or dir is exits sh ...

  2. Redis四(Set操作)

    1.Set操作 Set集合就是不允许重复的列表 集合操作(无序) sadd(name,values) 1 # name对应的集合中添加元素 scard(name) 1 获取name对应的集合中元素个数 ...

  3. appium 自动化测试案例

    原文地址http://www.cnblogs.com/tobecrazy/p/4579631.html 原文地址http://www.cnblogs.com/tobecrazy/ 该博主有很多干货,可 ...

  4. go——切片

    切片(slice)可以看作一种对数组的包装形式,它包装的数组为该切片的底层数组.反过来讲,切片是针对其底层数组中某个连续片段的描述,下面的代码声明了一个切片类型的变量: var ips = []str ...

  5. Python(模块(modue)、包(package))

    ''' 一 模块 模块一共三种: python标准库 第三方模块 应用程序自定义模块 模块两种执行方式: 1 用于启动执行 2 用于被调用执行 key:import module: 将执行文件(mod ...

  6. js, 树状菜单隐藏显示

    js写的不是很严谨~~~嘿嘿   <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...

  7. Java并发(7):阻塞队列

    在前面我们接触的队列都是非阻塞队列,比如PriorityQueue.LinkedList(LinkedList是双向链表,它实现了Dequeue接口). 使用非阻塞队列的时候有一个很大问题就是:它不会 ...

  8. akka框架地址

    http://doc.akka.io/docs/akka/2.2.3/AkkaJava.pdf

  9. SVN插件下载地址及更新地址

    SVN插件下载地址及更新地址,你根据需要选择你需要的版本.现在最新是1.8.xLinks for 1.8.x Release:Eclipse update site URL: http://subcl ...

  10. Shell awk 求标准差

    cat > temp000180255798957892187719 awk '{x[NR]=$0; s+=$0; n++} END{a=s/n; for (i in x){ss += (x[i ...