这个题目非常好。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的更多相关文章

  1. 【leetcode】234. Palindrome Linked List

    234. Palindrome Linked List 1. 使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点.如果是偶数个数 ...

  2. 【LeetCode】234. Palindrome Linked List (2 solutions)

    Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could ...

  3. 【LeetCode】234. Palindrome Linked List 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 234. Palindrome Linked List - LeetCode

    Question 234. Palindrome Linked List Solution 题目大意:给一个链表,判断是该链表中的元素组成的串是否回文 思路:遍历链表添加到一个list中,再遍历lis ...

  5. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

  6. [LeetCode] 234. Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  7. 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 ...

  8. (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 ...

  9. 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) ...

随机推荐

  1. 【leetcode刷题笔记】3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  2. <再看TCP/IP第一卷>关于网络层及协议细节---IP协议(2)--移动IP及DHCP

    题外话:本来想按照互联网的层级自下向上回顾这些协议的,但是今天实在得破个例,DHCP不得不说! 主机从一个网络迁移到另一个网络,那么IP编址的结构就要发生改变,当今主流有如下几种修改方案: (一)改变 ...

  3. SpringCloud之Eureka高可用集群环境搭建

    注册中心集群 在微服务中,注册中心非常核心,可以实现服务治理,如果一旦注册出现故障的时候,可能会导致整个微服务无法访问,在这时候就需要对注册中心实现高可用集群模式. Eureka集群相当简单:相互注册 ...

  4. 算法(Algorithms)第4版 练习 2.2.23

    测试结果: 算法(Algorithms)第4版 练习 2.2.10 算法(Algorithms)第4版 练习 2.2.11(1) 算法(Algorithms)第4版 练习 2.2.11(2) 算法(A ...

  5. cmd(或者说DOS窗口)输出内容到文件

    格式是:command >> filefullpath 格式是:DOS命令>>文件名全路径 举例说明: dir *.* >> D:\abc.txt dir *.* ...

  6. 洛谷p3369 treap

    这是一个treap裸题,还可以用splay,替罪羊树,线段树等等写 treap是树和堆结合,可以方便的在O(log(n))期望时间内进行以下操作,因此treap又叫做名次树 插入x数 删除x数(若有多 ...

  7. ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中

    1._all 1.1_all field _all字段是一个很少用到的字段,它连接所有字段的值构成一个用空格(space)分隔的大string,该string被analyzed和index,但是不被s ...

  8. vmware在桥接模式下配置centos7网络

    首先要将Vmware10.0.3设置为桥接模式. CentOS 7.0默认安装好之后是没有自动开启网络连接的! cd  /etc/sysconfig/network-scripts/  #进入网络配置 ...

  9. vi 常用编辑命令

    什么是vi: vi是Linux/Unix底下最常用的文本编辑器,可以理解为和Windows下的txt一样,咱们一般操作linux服务器的时候都是没有图形化界面的, 怎么移动光标,到哪个位置,替换修改什 ...

  10. BEC listen and translation exercise 40

    However, recently there's been more and more interest in the development of ostrich farming in other ...