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 and O(1) space?
题目标签:Linked List
题目给了我们一个 linked list,让我们判断它是不是回文。
这里可以利用 #206 Reverse Linked List 把右边一半的链表 倒转,然后从左右两头开始比较链表是否是回文。
这样的话,首先要找到链表的中间点,然后开始倒转右半边链表。
可以利用slow fast pointers 来找到中间点,当fast 走完的时候,slow 正好在中间。
Java Solution:
Runtime beats 39.01%
完成日期:06/11/2017
关键词:singly-linked list
关键点:利用slow fast 指针来找到中间点
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution
{
public boolean isPalindrome(ListNode head)
{
ListNode slow = head;
ListNode fast = head;
ListNode tail; // find the middle
while(fast != null && fast.next != null)
{
slow = slow.next;
fast = fast.next.next;
} if(fast != null) // odd length
slow = slow.next; // move middle to right half // reverse right half
tail = reverse(slow); // compare head and tail
while(tail != null)
{
if(head.val != tail.val)
return false; head = head.next;
tail = tail.next;
} return true;
} private ListNode reverse(ListNode cursor)
{
ListNode pre = null;
ListNode next; while(cursor != null)
{
next = cursor.next;
cursor.next = pre;
pre = cursor;
cursor = next;
} return pre; // tail node
}
}
参考资料:https://discuss.leetcode.com/topic/33376/java-easy-to-understand
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 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 ...
- 234 Palindrome Linked List 回文链表
请检查一个链表是否为回文链表. 进阶:你能在 O(n) 的时间和 O(1) 的额外空间中做到吗? 详见:https://leetcode.com/problems/palindrome-linked- ...
- [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面试题 02.06. 回文链表,解题心路
目录 leetcode面试题 02.06. 回文链表,解题心路 1.题目描述 2.java语言题解一 3.java语言题解二 4.C语言题解一 leetcode面试题 02.06. 回文链表,解题心路 ...
- LeetCode 234. Palindrome Linked List(判断是否为回文链表)
题意:判断是否为回文链表,要求时间复杂度O(n),空间复杂度O(1). 分析: (1)利用快慢指针找到链表的中心 (2)进行步骤(1)的过程中,对前半部分链表进行反转 (3)如果链表长是偶数,首先比较 ...
- LeetCode 234 Palindrome Linked List(回文链表)(*)(?)
翻译 给定一个单链表,确定它是否是回文的. 跟进: 你能够在O(n)时间和O(1)空间下完毕它吗? 原文 Given a singly linked list, determine if it is ...
- Leetcode 234 Palindrome Linked List 链表
判断链表是否是回文. 我直接将链表的一半进行倒置,然后将两半的链表进行比较 /** * Definition for singly-linked list. * struct ListNode { * ...
随机推荐
- 使用QTP录制自带Flight小实例
1.双击打开QTP10.0,启动过程中测试类型选择“WEB”. 2.进入主界面,New——Test,新建一个测试用例. 3.点击Record按钮,Record and settings对话框中,可以选 ...
- git常用命令和github
工作区:就是你的工作目录 暂存区:它像个缓存区域,临时保存你的改动 版本区:就是你的git仓库 HEAD:相当于一个指针,指向你最近一次提交后的结果 git status 查看状态 git add . ...
- PHPStorm+XDebug进行调试
笔者的开发环境如下: Windows8.1+Apache+PhpStorm+XDebug+Firefox(XDebug helper 1.4.3插件). 一.XDebug安装配置 (1)下载XDebu ...
- 动软生成器添加Mysql注释
1.解决没有mysql注释问题 替换原文件下载地址 2.更新Models模板 <#@ template language="c#" HostSpecific="Tr ...
- (转)全文检索技术学习(一)——Lucene的介绍
http://blog.csdn.net/yerenyuan_pku/article/details/72582979 本文我将为大家讲解全文检索技术——Lucene,现在这个技术用到的比较多,我觉得 ...
- SDK _ 静态控件的使用
静态控件的使用 静态控件主要区分两种使用方式:文本 \ 图片 在使用静态控件的时候,ID始终默认为 IDC_STATIC,需要进行更改 怎样通过可视化编程显示一张图片 需要添加一个位图资源 需要添加一 ...
- 09C语言指针
C语言指针 地址 地址就是数据元素在内存中的位置表示: &变量名 #include <stdio.h> int main(){ int aa; unsigned int bb = ...
- python多进程和多线程编程
17 多线程和多进程并发 The modules described in this chapter provide support for concurrent execution of code. ...
- NOIP 前的垂死挣扎
计划每天十题吧,可能会一天水题一天难题吧.题目以杂题为主,没有专题可言. 10.11 计划: [x] P2939 [USACO09FEB] 改造路 Revamping Trails [ ] P3601 ...
- ubuntu root用户登陆
sudo vi /etc/lightdm/lightdm.conf (如果没有该文件则创建,内容如下) [SeatDefaults] user-session=ubuntu greeter-ses ...