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 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(回文链表判断)的更多相关文章
- [LeetCode] 234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- [CareerCup] 2.7 Palindrome Linked List 回文链表
2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...
- [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 ...
- 234 Palindrome Linked List 回文链表
请检查一个链表是否为回文链表. 进阶:你能在 O(n) 的时间和 O(1) 的额外空间中做到吗? 详见:https://leetcode.com/problems/palindrome-linked- ...
- lintcode 中等题:Palindrome Linked List 回文链表
题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的 ...
- LeetCode OJ Palindrome Number(回文数)
class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...
- LeetCode 9 Palindrome Number(回文数字判断)
Long Time No See ! 题目链接https://leetcode.com/problems/palindrome-number/?tab=Description 首先确定该数字的 ...
- leetcode面试题 02.06. 回文链表,解题心路
目录 leetcode面试题 02.06. 回文链表,解题心路 1.题目描述 2.java语言题解一 3.java语言题解二 4.C语言题解一 leetcode面试题 02.06. 回文链表,解题心路 ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
随机推荐
- Android开发中string.xml文件的使用
为什么需要把应用中出现的文字单独存放在string.xml文中呢? 一:是为了国际化,Android建议将在屏幕上显示的文字定义在strings.xml中,如果今后需要进行国际化,比如我们开发的应用本 ...
- Android Studio设置行宽、格式化断行
设置基于Android studio 1.2,其它版本可能位置不大一样,可以直接搜索 1.设置行宽 就是那条右标准线的位置:Setting-->Editor-->Code Style,右侧 ...
- Sqrt(x)
这题没多大技巧性,只是牛顿迭代法多用于数值计算,这里出现有些意外.维基上有方法说明:http://zh.wikipedia.org/wiki/牛顿法 int sqrt(int x) { if (x = ...
- beego——多种格式的数据输出
beego当初设计的时候就考虑了API功能的设计,而我们在设计API的时候经常是输出JSON或者XML数据,那么beego提供了这样的方式直接输出: 1.JSON格式输出 func (this *Ad ...
- beego——获取参数
1.获取参数 我们经常需要获取用户传递的数据,包括Get.POST等方式的请求,beego里面会自动解析这些数据,你可以通过如下方式获取数据: GetString(key string) string ...
- DP专题·二
1.hdu 1260 Tickets 题意:有k个人,售票员可以选择一个人卖,或者同时卖给相邻的两个人.问最少的售票时间. 思路:dp[i] = min(dp[i - 1] + singlep[i], ...
- Eclipse 多行注释选择
1.Eclipse 中的多行注释 选择与清除 (?s)\/\*\*.*?\*\/ (?s)可以匹配多行 \/\*\*表示以/**开头 匹配类似 /** * * * * asdfasdf */
- 【Java】Swing+IO流实现一个简单的文件加密程序(较完整版)
留着参考 beans package com.my.bean; import java.io.Serializable; public class EncryptedFile implements S ...
- Secondary ,Supplementary alignment 和bwa mem的-M -Y参数
1.supplementary alignment supplementary alignment是指一条read的一部分和参考区域1比对成功,另一部分和参考区域2比对成功,参考区域1和参考区域2没有 ...
- HDU 1337 && POJ 1218&& zju 1350 方法总结
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1337 杭电 http://poj.org/problem?id=1218清华 http://acm.zj ...