Description:

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?

判断一个单向链表是不是回文。

思路:

最简单的思路就是,用一个遍历一遍,存下来,再遍历反着做比较。时间复杂度O(n),空间复杂度O(n)。竟然能过。。。。。。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode l; public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null) {
return true;
} List<Integer> nodeList = new ArrayList<>();
ListNode it = head;
while(it!=null) {
nodeList.add(it.val);
it = it.next;
}
boolean res = true;
for(int i=nodeList.size()-1; i>=nodeList.size()/2; i--) {
if(nodeList.get(i)!=head.val) {
res = false;
break;
}
else {
head = head.next;
}
} return res; } }

  想了想既然这样不如用递归,递归和栈类似,遍历两遍就OK,但是但是没想错的话空间复杂度还是O(n)吧,虽然没有显示的来new出空间。

要想空间复杂度为O(1)现在能想到的只有找到链表中点,就地逆置单链表了。

LeetCode——Palindrome Linked List的更多相关文章

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

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

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

  4. 【leetcode】234. Palindrome Linked List

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

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

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

  6. 234. Palindrome Linked List - LeetCode

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

  7. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  8. [LeetCode] Palindrome Number 验证回文数字

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

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

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

随机推荐

  1. LPD打印机服务是什么意思

    line Printer Daemon(LPD)行式打印机后台程序,用于假脱机打印工作的UNIX后台程序(后台服务).行式打印机后台程序是一个安装在UNIX打印服务器上的后台程序.它的功能是等待接受客 ...

  2. iOS边练边学--介绍布局的三种方法

    使用代码实现Autolayout的方法1- 创建约束 +(id)constraintWithItem:(id)view1attribute:(NSLayoutAttribute)attr1relate ...

  3. Applet Mode

    https://github.com/threerings/getdown/wiki/Applet-Mode ————————————————————————————————————————————— ...

  4. [ Laravel 5.1 文档 ] 服务 —— 任务调度

    1.简介 在以前,开发者需要为每一个需要调度的任务编写一个Cron条目,这是很让人头疼的事.你的任务调度不在源码控制中,你必须使用SSH登录到服务器然后添加这些Cron条目.Laravel命令调度器允 ...

  5. 判断IE浏览器的最简洁方法

    <script type='text/javascript'> var ie = !-[1,]; alert(ie);</script>

  6. 【R】用 ggplot2 绘制漂亮的分级统计地图

    最近我一直尝试利用R绘制地图,我从网上找到了上百种不同的实现方法,然而其中却没有适用于我的数据的方法.最终,我从以下几个博客[1]中找到了灵感.我在整合这些资源的基础上,通过不断的试验和修正得到了一个 ...

  7. Flume1.5.0入门:安装、部署、及flume的案例

    转自:http://www.aboutyun.com/thread-8917-1-1.html 问题导读1.什么是flume2.flume的官方网站在哪里?3.flume有哪些术语?4.如何配置flu ...

  8. 普通windows版本安装winServer的特色功能 以dedup功能为展示点

    安装 Windows 功能角色 1.选择安装源 在 Windows 8.1 系统上不存在重复数据删除功能,需要从对应的服务器版本,即 Windows Server 2012 R2 上提取相关文件. 2 ...

  9. vs2008 x64编译环境 忽略了 #ifdef WIN32

    解决方法: 右键项目属性,在预处理器中添加WIN32即可 效果:

  10. ffmpeg h264+ts +(sdl)显示方式

    网友: 明月惊鹊(357161826) 2014-1-16 10:07:00ffmpeg + sdl一米阳光(740053660) 2014-1-16 10:08:29Simple DirectMed ...