234 Palindrome Linked List 回文链表
请检查一个链表是否为回文链表。
进阶:
你能在 O(n) 的时间和 O(1) 的额外空间中做到吗?
详见:https://leetcode.com/problems/palindrome-linked-list/description/
Java实现:
方法一:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
//0个节点或是1个节点
if(head==null||head!=null&&head.next==null){
return true;
}
ListNode slow=head;
ListNode fast=head;
Stack<Integer> stk=new Stack<Integer>();
stk.push(head.val);
while(fast.next!=null&&fast.next.next!=null){
slow=slow.next;
fast=fast.next.next;
stk.push(slow.val);
}
//链表长度为偶数
if(fast.next!=null){
slow=slow.next;
}
while(slow!=null){
int tmp=stk.pop();
if(slow.val!=tmp){
return false;
}
slow=slow.next;
}
return true;
}
}
方法二:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
//0个节点或是1个节点
if(head==null||head!=null&&head.next==null){
return true;
}
ListNode slow=head;
ListNode fast=head;
ListNode first=null;
while(fast!=null&&fast.next!=null){
first=slow;
slow=slow.next;
fast=fast.next.next;
}
fast=first.next;
first.next=null;
ListNode pre=null;
ListNode next=null;
while(fast!=null){
next=fast.next;
fast.next=pre;
pre=fast;
fast=next;
}
first=head;
fast=pre;
while(first!=null&&fast!=null){
if(first.val!=fast.val){
return false;
}
first=first.next;
fast=fast.next;
}
return true;
}
}
C++实现:
方法一:
/**
* 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==nullptr||head->next==nullptr)
{
return true;
}
ListNode *slow=head;
ListNode *fast=head;
stack<int> stk;
stk.push(head->val);
while(fast->next&&fast->next->next)
{
slow=slow->next;
fast=fast->next->next;
stk.push(slow->val);
}
if(!fast->next)
{
stk.pop();
}
while(slow->next)
{
slow=slow->next;
int tmp=stk.top();
if(slow->val!=tmp)
{
return false;
}
stk.pop();
}
return true;
}
};
方法二:
/**
* 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||!head->next)
{
return true;
}
ListNode *slow=head;
ListNode *fast=head;
ListNode *first=nullptr;
while(fast&&fast->next)
{
first=slow;
slow=slow->next;
fast=fast->next->next;
}
fast=first->next;
first->next=nullptr;
ListNode *pre=nullptr;
ListNode *next=nullptr;
while(fast)
{
next=fast->next;
fast->next=pre;
pre=fast;
fast=next;
}
first=head,fast=pre;
while(first&&fast)
{
if(first->val!=fast->val)
{
return false;
}
first=first->next;
fast=fast->next;
}
return true;
}
};
参考:https://www.cnblogs.com/grandyang/p/4635425.html
234 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 ...
- lintcode 中等题:Palindrome Linked List 回文链表
题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的 ...
- LeetCode 234:回文链表 Palindrome Linked List
请判断一个链表是否为回文链表. Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: ...
- LeetCode 234. Palindrome Linked List(判断是否为回文链表)
题意:判断是否为回文链表,要求时间复杂度O(n),空间复杂度O(1). 分析: (1)利用快慢指针找到链表的中心 (2)进行步骤(1)的过程中,对前半部分链表进行反转 (3)如果链表长是偶数,首先比较 ...
- [Swift]LeetCode234. 回文链表 | 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 ...
- 如何判断一个单向链表是否为回文链表(Palindrome Linked List)
题目:给定一个单向链表,判断它是不是回文链表(即从前往后读和从后往前读是一样的).原题见下图,还要求了O(n)的时间复杂度O(1)的空间复杂度. 我的思考: 1,一看到这个题目,大脑马上想到的解决方案 ...
随机推荐
- Linux下汇编语言学习笔记57 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- Codeforces 645C Enduring Exodus【二分】
题目链接: http://codeforces.com/contest/645/problem/C 题意: 给定01串,将k头牛和农夫放进, 0表示可以放进,1表示不可放进,求农夫距离其牛的最大距离的 ...
- POJ 3684_Physics Experiment
题意: 若干球最初从高到低排列,依次落下. 球与地面碰撞,速度不变方向相反,球之间碰撞, 交换速度和方向.问某一时刻各个球的高度. 分析: 把球之间的碰撞看成是擦肩而过,但是由于半径的存在,最后每个球 ...
- poj——2367 Genealogical tree
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6025 Accepted: 3969 ...
- 学习日常笔记<day10>servlet编程
1 如何开发一个Servlet 1.1 步骤: 1)编写java类,继承HttpServlet类 2)重新doGet和doPost方法 3)Servlet程序交给tomcat服务器运行!! 3.1 s ...
- Servlet发送邮件
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/sending-email.html: 使用Servlet发送一封电子邮件是非常简单的,但是开始之 ...
- jquery校验框架
http://www.validform.club/ http://craftpip.github.io/jquery-confirm/
- Open Flash Chart IO ERROR Loading test data Error #2032
http://blog.sina.com.cn/s/blog_6754464e0100qfvd.html Open Flash Chart 2 提示Open Flash Chart IO ERROR ...
- iOS开发项目实战——Swift实现图片轮播与浏览
近期開始开发一个新的iOS应用,自己决定使用Swift.进行了几天之后,发现了一个非常严峻的问题.那就是无论是书籍,还是网络资源,关于Swift的实在是太少了,随便一搜全都是OC实现某某某功能.就算是 ...
- tomcat用80port能够启动,可是浏览器不显示tomcat首页
一.打开执行(ctrl+r)->输入cmd->确定->输入netstat -ano 结果检測到 :80port被system 占用,如图所看到的 打开进程发现确实被 PID为 4 的 ...