作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/palindrome-linked-list/#/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)不符合要求。但是能通过。

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. public class Solution {
  10. public boolean isPalindrome(ListNode head) {
  11. if(head == null || head.next == null){
  12. return true;
  13. }
  14. ListNode temp = head;
  15. Stack<Integer> stack = new Stack<Integer>();
  16. while(temp != null){
  17. stack.push(temp.val);
  18. temp = temp.next;
  19. }
  20. while(head != null){
  21. int top = stack.pop();
  22. if(top != head.val){
  23. return false;
  24. }
  25. head = head.next;
  26. }
  27. return true;
  28. }
  29. }

二刷,Python。

同样使用了数组,但是判断回文的时候,直接使用的双指针从头和尾向中间遍历。

  1. # Definition for singly-linked list.
  2. # class ListNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6. class Solution(object):
  7. def isPalindrome(self, head):
  8. """
  9. :type head: ListNode
  10. :rtype: bool
  11. """
  12. vals = []
  13. while head:
  14. vals.append(head.val)
  15. head = head.next
  16. N = len(vals)
  17. left, right = 0, N - 1
  18. while left < right:
  19. if vals[left] != vals[right]:
  20. return False
  21. left += 1
  22. right -= 1
  23. return True

日期

2017 年 5 月 21 日
2018 年 11 月 24 日 —— 周六快乐

【LeetCode】234. Palindrome Linked List 解题报告(Python)的更多相关文章

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

  2. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  3. 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)

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

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

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

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

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

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

  8. LeetCode 234 Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...

  9. LeetCode 206 Reverse Linked List 解题报告

    题目要求 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5-> ...

随机推荐

  1. 毕业设计之zabbix+微信企业号报警

    需要自己申请一个微信企业号 创建应用 AgentId 1000003 Secret SOI8b20G96yUVM29K02-bP5N5o6dovwSF2RrDaXHJNg 企业ID(自己再企业信息里面 ...

  2. MariaDB——在Linux中查找数据库路径,并进入数据库

    1.直接在命令行运营mysql,如果出现下图,说明数据库路径没有设置到环境变量里. 2.找出数据库路径,可以用ps -ef | grep my 命令,查找后台正在执行的命令任务中,包含my字母开头的所 ...

  3. android listview展示图片

    最近学习android开发,感触颇多,和网站开发对比,还是有很大的差距,在这里记录一下. android listview展示图片 在网站开发上,展示图片非常简单,一个HTML img标签就搞定,加上 ...

  4. java类加载、对象创建过程

    类加载过程: 1, JVM会先去方法区中找有没有相应类的.class存在.如果有,就直接使用:如果没有,则把相关类的.class加载到方法区 2, 在.class加载到方法区时,会分为两部分加载:先加 ...

  5. Elaticsearch(一)--基础原理及用法

    一.基础概念 1.Elasticsearch简介 Lucene是Java语言编写的全文(全部的文本内容进行分析,建立索引,使之可以被搜索)检索引擎工具包(全文检索引擎的架构),用于处理纯文本的数据,提 ...

  6. day13 iptables防火墙

    day13 iptables防火墙 一.防火墙的概述 1.什么是防火墙 防止恶意流量访问的软件就叫做防火墙. 2.防火墙的种类 软件防火墙:firewalld.iptables 硬件防火墙:F5 fi ...

  7. 【并发编程】Java并发编程-看懂AQS的前世今生

    在我们可以深入学习AbstractQueuedSynchronizer(AQS)之前,必须具备了volatile.CAS和模板方法设计模式的知识,本文主要想从AQS的产生背景.设计和结构.源代码实现及 ...

  8. JavaIO——内存操作流、打印流

    我们之前所做的都是对文件进行IO处理,实则我们也可以对内存进行IO处理.我们将发生在内存中的IO处理称为内存流. 内存操作流也可分为两类:字节内存流和字符内存流. (1)ByteArrayInputS ...

  9. 在隐藏导航栏的控制器中,调用UIIMagePickerController,出现导航栏变透明的问题

    在隐藏导航栏的控制器中,调用UIIMagePickerController,出现导航栏变透明的问题 解决办法 #pragma mark - UIImagePickerController Delega ...

  10. 【Spring Framework】Spring入门教程(四)注册Bean到IOC容器

    注册Bean到IOC容器大致分为4种: ①.包扫描+组件注解(@Controller.@Service.@Repository.@Component) 针对类是我们自己编写的情况 ②.@Bean注解 ...