Question

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?

Solution

这一题思路并不难。要满足follow-up的要求,我们用到了快慢指针。

1. 用快慢指针得到前后两半list,这里有个技巧是quick先判断有无next,slow再走。这样就保证slow永远指向后半部分的前一个结点

2. Reverse 后半部分的list。三指针方法

3. 比较前半链表和反转后的后半链表

思路虽不难,但是要做到bug-free还是有难度。关键在于对以上每个子问题都熟悉。

 # Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if head is None or head.next is None:
return True
slow = head
quick = head
while quick.next is not None:
quick = quick.next
if quick.next is not None:
quick = quick.next
slow = slow.next
# Reverse sub list between slow.next and quick
cur = slow.next
slow.next = None
then = cur.next
cur.next = None
while then is not None:
tmp = then.next
then.next = cur
cur = then
then = tmp
second_head = cur
# Compare first sub list and second sub list
cur1 = head
cur2 = second_head
while cur1 is not None and cur2 is not None:
if cur1.val != cur2.val:
return False
cur1 = cur1.next
cur2 = cur2.next
return True

Palindrome Linked List 解答的更多相关文章

  1. [CareerCup] 2.7 Palindrome Linked List 回文链表

    2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...

  2. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

  3. 【leetcode】234. Palindrome Linked List

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

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

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

  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

    234. Palindrome Linked List Easy Given a singly linked list, determine if it is a palindrome. Exampl ...

  7. 234. Palindrome Linked List - LeetCode

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

  8. LeetCode 234:回文链表 Palindrome Linked List

    ​ 请判断一个链表是否为回文链表. Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: ...

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

随机推荐

  1. Java 四种线程池的用法分析

    1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? new Thread(new Runnable() { @Override public void run() { ...

  2. 为什么Nginx的性能要比Apache高很多?

    为什么Nginx的性能要比Apache高很多? 这得益于Nginx使用了最新的epoll(Linux 2.6内核)和kqueue(freebsd)网络I/O模型,而Apache则使用的是传统的sele ...

  3. 单片微机原理P4:80C51串口与串行总线拓展

    0. 串口通讯 0. 串口通讯的数据传输方式:单工(单向传输数据),半双工(非同时双向传输),全双工(同时,双向传输) 1. 根据通信方式的不同又分为同步通讯和异步通讯. 同步通讯:所有设备都使用同一 ...

  4. 实现jquery EasyUI tabs选项卡关闭图标后载入自己定义事件

    当关闭tabs选项卡时,底部footer须要通过javascript又一次定位calcFooter(),怎样实现呢?选项卡上的关闭图标的方法是easyui自带的,calcFooter()写在onClo ...

  5. 把自己的程序打成jar包,让别人调用

     我们写程序的时候往往需要把自己的程序打包成jar包,给第三方调用.Eclipse让我们非常方便的可以导出jar包.但是当程序里需要用到res里的资源时,往往就会出现问题.因为统自动生成的R类如果被打 ...

  6. Enabling Active Directory Authentication for VMWare Server running on Linux《转载》

    Enabling Active Directory Authentication for VMWare Server running on Linux Version 0.2 - Adam Breid ...

  7. 关于transform的2D

    在transform的学习中,自己总结了一点经验. 我们知道transform有2D和3D的两大类变换,这里分享下关于2D的属性简单示例: 一.2D变换:  (x为水平,y为垂直) 1.skew(斜拉 ...

  8. Socket与TcpClient的区别(转载)

    Socket和TcpClient有什么区别 原文:http://wxwinter.spaces.live.com/blog/cns!C36588978AFC344A!322.entry 回答: &qu ...

  9. Solr导入数据库数据

    接Solr-4.10.2与Tomcat整合.1.在solrconfig.xml中添加数据导入节点,solrconfig.xml路径为D:\solr\data\solr\collection1\conf ...

  10. Js 时间间隔计算(间隔天数)

    function GetDateDiff(startDate,endDate)  {      var startTime = new Date(Date.parse(startDate.replac ...