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 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的更多相关文章
- [LeetCode] 234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- 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 ...
- 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 ...
- (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 ...
- 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) ...
- LeetCode 234 Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...
- [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 ...
- LeetCode(55)- Palindrome Linked List
题目: Given a singly linked list, determine if it is a palindrome. Follow up: 思路: 题意:判断一个链表是不是回文 利用两个指 ...
- LeetCode 234 Palindrome Linked List(回文链表)(*)(?)
翻译 给定一个单链表,确定它是否是回文的. 跟进: 你能够在O(n)时间和O(1)空间下完毕它吗? 原文 Given a singly linked list, determine if it is ...
随机推荐
- NYOJ-244 16进制的简单运算 AC 分类: NYOJ 2014-01-17 21:11 195人阅读 评论(0) 收藏
#include<stdio.h> int main() { long x,y; char op; int t; scanf("%d ", &t); while ...
- jquery ajax/post/get 传参数给 mvc的action
jquery ajax/post/get 传参数给 mvc的action1.ActionResult Test1 2.View Test1.aspx3.ajax page4.MetaObjec ...
- VC5509的通用GEL代码
GEL是通用扩展语言(General Extension Language)的英文缩写,GEL是一个大小写敏感但缺少类型检测的解释性语言,只有int类型,在语法上可看作是C语言的一个子集.GEL主要用 ...
- Win7无法使用VPN的原因与解决方法(一)
如果Windows 7不是通过正常安装途径的话,像Ghost错误.系统环境改变等,都有可能导致无法使用VPN,而且由于原因不同,给出的提示也不尽相同.实际上,万变不离其宗, VPN是要依靠Window ...
- MFC单文档程序结构
MFC单文档程序结构三方面: Doc MainFrame View
- HDU5008 Boring String Problem(后缀数组)
练习一下字符串,做一下这道题. 首先是关于一个字符串有多少不同子串的问题,串由小到大排起序来应该是按照sa[i]的顺序排出来的产生的. 好像abbacd,排序出来的后缀是这样的 1---abbacd ...
- ZOJ 3555 Ice Climber(dp)
晦涩的题意+各种傻逼害我调了那么久,实际上题目就是一个dp[i][j],dp[i][j]表示第i层第j个最少需要多少时间,当我们去更新dp[i][j]的时候,考虑的是从第i+1层的某一个dp[i+1] ...
- java基础知识回顾之抽象类
/* 抽象类: 抽象:笼统,模糊,看不懂!不具体. 特点: 1,方法只有声明没有实现时,该方法就是抽象方法,需要被abstract修饰. 抽象方法必须定义在抽象类中.该类必须也被abstract修饰. ...
- Good Bye 2015 C. New Year and Domino 二维前缀
C. New Year and Domino They say "years are like dominoes, tumbling one after the other". ...
- ARM 汇编指令
ARM汇编程序特点: l 所有运算处理都是发生通用寄存器(一般是R0~R14)的之中.所有存储器空间(如C语言变量的本质就是一个存储器空间上的几个BYTE).的值的处理,都是要传送到通 ...