leetcode:234. Palindrome Linked List

这个题目非常好。http://blog.csdn.net/u012249528/article/details/47124771给出了三种解法,其中前两个是不满足条件的,不过具有参考价值:
第一种办法:用数组倒着存前半段的链表的值,然后和后半段链表的值进行比较。这种解法运行的时间最久可能是因为数组倒着插入比较耗时。
第二种解法:在第一种的思路的基础上,我们要实现一个倒序,我们干嘛不用现成的数据结构-栈,于是把链表前半段压栈,然后出栈和后面的链表依次比较,这种运行时间最短,但因为用到了栈还是不符合题目要求。
第三种:我们这样想,我们可不可以不借助外在的存储实现倒序呢,其实是可以的,链表反转的时候我们就没有借助外在存储。思路是把后半段的原地链表反转然后和前半段进行比较(当然你也可以反转前半段)运行时间稍微比第二种慢一些,但是符合题目O(1)空间复杂度的要求
本题解决代码参考:https://discuss.leetcode.com/topic/33376/java-easy-to-understand
public boolean isPalindrome(ListNode head) {
ListNode slow = head;
ListNode fast = head;
if(head==null||head.next==null){
return true;
}
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
if (fast != null) { // odd nodes: let right half smaller 不用貌似也行
slow = slow.next;
}
//对后半段反转
slow = reverse(slow);
fast = head;
while(slow!=null){
if(fast.val!=slow.val){
return false;
}
fast = fast.next;
slow = slow.next;
}
return true;
}
public ListNode reverse(ListNode head){
ListNode pre = null;
while(head != null){
ListNode tmp = head.next;
head.next = pre;
pre = head;
head = tmp;
}
return pre;
}
leetcode:234. Palindrome Linked List的更多相关文章
- 【leetcode】234. Palindrome Linked List
234. Palindrome Linked List 1. 使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点.如果是偶数个数 ...
- 【LeetCode】234. Palindrome Linked List (2 solutions)
Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could ...
- 【LeetCode】234. Palindrome Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 234. Palindrome Linked List - LeetCode
Question 234. Palindrome Linked List Solution 题目大意:给一个链表,判断是该链表中的元素组成的串是否回文 思路:遍历链表添加到一个list中,再遍历lis ...
- 234. Palindrome Linked List【easy】
234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...
- [LeetCode] 234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- 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) ...
随机推荐
- linux下无线鼠标驱动执行流程
操作系统: debian 7.4(linux 3.2.54) 硬件: 一个无线鼠标.一个有线鼠标.usb集线器. 从淘宝上花了15块钱买了个无线鼠标,很好奇它的驱动程序是如何执行的. 首先将usb集线 ...
- shell脚本默认变量值
脚本参数相关: $# 是传给脚本的参数个数 $ 是脚本本身的名字 $ 是传递给该shell脚本的第一个参数 $ 是传递给该shell脚本的第二个参数 $@ 是传给脚本的所有参数的列表 $* 是以一个单 ...
- Linux电源管理(1)-整体架构【转】
本文转载自:http://www.wowotech.net/pm_subsystem/pm_architecture.html 1. 前言 在这个世界中,任何系统的运转都需要能量.如树木依靠光能生长, ...
- ios9 3dtouch 博客
http://my.oschina.net/u/2340880/blog/511509#OSC_h3_3
- uitableview 刷新一行
ios UITableview 刷新某一行 或 section //一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWith ...
- nginx+tomcat负载均衡搭建
一. 单独部署tomcat和nginx Nginx版本:nginx-1.13.5 Tomcat版本:apache-tomcat-8.5.8 操作系统:win10 必须先部署一个tomcat服 ...
- Spark- Transformation实战
RDD的算子分为两类,是 Trans formation(Lazy),一类是 Action(触发任务执行RDD不存在真正要计算的数据,而是记录了RDD的转换关系(调用了什么方法,传入什么函数) RDD ...
- cscope usage
1) Reference: Linux 平台下阅读源码的工具链 程序员的利器 – cscope 2) cscope help: :help cscope :help cscope-suggestion ...
- EAV模型
了解EAV设计基本原理的最好方法就是理解行建模(row modelling,其中EAV是广义形式). 以一超市数据库为例,必须管理数以千计的产品和品牌,其中许多产品存在期很短暂.那么,显而易见,产品名 ...
- C++ 0X 新特性实例(比较常用的) (转)
转自:http://www.cnblogs.com/mrblue/p/3141456.html //array #include <array> void Foo1() { array&l ...