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,一看到这个题目,大脑马上想到的解决方案 ...
随机推荐
- Codeforces 645B Mischievous Mess Makers【逆序数】
题目链接: http://codeforces.com/problemset/problem/645/B 题意: 给定步数和排列,每步可以交换两个数,问最后逆序数最多是多少对? 分析: 看例子就能看出 ...
- POJ 3686_The Windy's
题意: N个工件要在M个工厂加工,一个工件必须在一个工厂做完,工厂一次只能处理一个工件.给定每个工件在每个工厂加工所需时间,求出每个工件加工结束的最小时间平均值. 分析: 工厂一次只能处理一个工件,那 ...
- restful接口就是url嘛,通过http请求发起访问。那接口进行监控,就可以监控这个restful url嘛
EasyAPI接口管理系统 专注API接口监控,让您的API接口更稳定,与APP更紧密 + 购买监控服务 接口性能分析 分析App对应的API接口请求性能,包含HTTP响应时间.吞吐率.HTTP错误率 ...
- jquery验证插件validate自定义扩展
<script src="${pageContext.request.contextPath}/resources/js/jquery-1.12.0.min.js" type ...
- Jenkins安装与使用
一.Jenkins简介 Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发布/测试项目. 2.监控外部调用执行的工作 二.下载与安装 下载地址 ...
- Resharper 实现接口的方式
- 异常机制及throw与throws的区别(转)
异常机制及throw与throws的区别 分类: Java2008-11-14 16:08 9672人阅读 评论(5) 收藏 举报 exceptionstringjavafunclass编译器 Jav ...
- IDEA 的使用(快捷键、括号对齐的方式)
Java IDE 工具不是只有一个 Eclipse,还有同样十分优秀的 IDEA. 0. 常用快捷键 查看与设置:[File]⇒ [Settings]⇒ [Keymap] back/forward:c ...
- python-----使用requirements.txt批量安装包
首先写一个 requirements.txt,格式如图: 然后使用命令行,到 requirements.txt 所在的目录下,执行命令: pip install -r requirements.txt ...
- Java:EL表达式
ylbtech-Java:EL表达式 EL(Expression Language) 是为了使JSP写起来更加简单.表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 ...