Palindrome Linked List 解答
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 解答的更多相关文章
- [CareerCup] 2.7 Palindrome Linked List 回文链表
2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...
- 【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 ...
- 【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 ...
- 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
234. Palindrome Linked List Easy Given a singly linked list, determine if it is a palindrome. Exampl ...
- 234. Palindrome Linked List - LeetCode
Question 234. Palindrome Linked List Solution 题目大意:给一个链表,判断是该链表中的元素组成的串是否回文 思路:遍历链表添加到一个list中,再遍历lis ...
- LeetCode 234:回文链表 Palindrome Linked List
请判断一个链表是否为回文链表. Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: ...
- [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 ...
随机推荐
- 算法导论(第三版) Exercises4.2(求最大和子数组的算法优化过程)
4.1-1 如所有元素都为负,则返回所有元素中最大的负数. 4.1-2(暴力法求最大和子数组) struct subarray { int start, end, sum; }; void brute ...
- Spring的工作原理核心组件和应用
Spring框架 Spring 是管理多个java类的容器框架,注意是类不管理接口. Spring 的主要功能 Ioc 反转控制和 DI 依赖注入. 注入的方式可以是构造函数赋值也可以是 set方法赋 ...
- Http(get,post)及HttpClient(get,post)的简单使用
1. 使用 Http 的 Get 方式读取网络数据 import java.io.BufferedReader; import java.io.IOException; import java.io. ...
- PHP设计模式笔记三:三种基本设计模式(工厂模式、单例模式、注册树模式) -- Rango韩老师 http://www.imooc.com/learn/236
一.工厂设计模式 index.php $db = IMooc\Factory::createDatabase(); 使用工厂类的静态方法直接创建一个dababase对象,当类名发生修改时,在工厂里修改 ...
- [转]使用Composer管理PHP依赖关系
简介 现在软件规模越来越大,PHP项目的开发模式和许多年前已经有了很大变化.记得初学PHP那会儿,boblog是一个很好的例子,几乎可以代表 PHP项目的开发模式.当时PHP 5.x以上的版本刚开始流 ...
- crtmpserver通常使用基本类演示
以前我们做了分析过程,这一次,我们都参与了类做梳子,两个可以一起关注一下一起合并,整个方案的实施是有帮助. BaseClientApplication APP基类,一切APP都基于这个类 Stream ...
- txt 分割程序
网上有很多 分割程序 ,但是他们都没有满足实际的用户要求 ,大家当然是希望看文章小说 一章节一章节的看 并非是那些传统意义上的按照文件的大小切割 所以 我特写本文研究下 这个简单的算法该怎样设计 说白 ...
- 原创:2016.4.25-2016.5.1 C# informal essay and tittle_tattle
1.Some tips of the Time in C sharp (1) How to define the category of the "Datetime"? date ...
- Java中的Clone机制(浅层复制)
浅层复制代码: import java.util.*; class Int{ private int i; public Int(int ii){i = ii;} public void increm ...
- Fedora下YouCompleteMe配置
需要在默认的.ycm_extra_conf.py 中添加(C++的路径可能需要根据版本号修改) '-isystem', '/usr/include', '-isystem', '/usr/includ ...