Given a singly linked list, determine if it is a palindrome.

Example 1:

  1. Input: 1->2
  2. Output: false

Example 2:

  1. Input: 1->2->2->1
  2. Output: true

Follow up:
Could you do it in O(n) time and O(1) space?

  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. class Solution {
  10. public boolean isPalindrome(ListNode head) {
  11. if(head == null || head.next == null) return true;
  12. //根据快慢指针求中点
  13. ListNode slow = head;
  14. ListNode fast = head;
  15. while(fast != null && fast.next != null) {
  16. slow = slow.next;
  17. fast = fast.next.next;
  18. }
  19. if(fast != null)
  20. slow = slow.next;
  21. //slow是中点,将中点反转后
  22. slow = reverse(slow);
  23. fast = head;
  24. while(slow != null) {
  25. if(slow.val != fast.val)
  26. return false;
  27. slow = slow.next;
  28. fast = fast.next;
  29.  
  30. }
  31. return true;
  32. }
  33. public ListNode reverse(ListNode head) {
  34. ListNode pre = null;
  35. ListNode next = null;
  36. while(head != null) {
  37. next = head.next;
  38. head.next = pre;
  39. pre = head;
  40. head = next;
  41. }
  42. return pre;
  43. }
  44. }

234. Palindrome Linked List【Easy】【判断链表是否回文】的更多相关文章

  1. LeetCode 234. Palindrome Linked List(判断是否为回文链表)

    题意:判断是否为回文链表,要求时间复杂度O(n),空间复杂度O(1). 分析: (1)利用快慢指针找到链表的中心 (2)进行步骤(1)的过程中,对前半部分链表进行反转 (3)如果链表长是偶数,首先比较 ...

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

  3. [leetcode] 234. Palindrome Linked List (easy)

    原题 回文 水题 function ListNode(val) { this.val = val; this.next = null; } /** * @param {ListNode} head * ...

  4. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

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

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

  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】234. Palindrome Linked List

    ques: 判断一个链表是否回文 Could you do it in O(n) time and O(1) space? method:先将链表分为两部分,将后半部分反转,最后从前往后判断是否相等. ...

  8. 【leetcode】234. Palindrome Linked List

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

  9. 234. Palindrome Linked List - LeetCode

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

随机推荐

  1. 51Nod 1094 和为k的连续区间 | 水

    Input示例 6 10 1 2 3 4 5 6 Output示例 1 4 #include "cstdio" #include "algorithm" #in ...

  2. 使用Docker搭建Django,Nginx,R,Python部署环境

    转载自https://blog.csdn.net/The_One_is_all/article/details/76063968 基本环境: Ubuntu 16.10 docker 17.06.0-c ...

  3. CAS 逻辑流程图

  4. Mybatis中select传递多个参数

    一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...

  5. Informatica _组件使用介绍及优化

    转载 http://blog.csdn.net/yongjian1092/article/details/52588434 有空自己会写一个关于这方面的文章.

  6. python学习笔记(八)之元组

    元组:和列表十分相似,可以说是一个受限的列表.最大的限制是,元组不能更改. 创建元组 >>> tuple1 = (123,'asd',(1,2,3)) >>> tu ...

  7. 程序员你为什么这么累? - Controller规范

    导读:程序员你为什么这么累? 接口定义:程序员你为什么这么累? - 接口定义 第一篇文章中,我贴了2段代码,第一个是原生态的,第2段是我指定了接口定义规范,使用AOP技术之后最终交付的代码,从15行到 ...

  8. springboot:Spring boot中mongodb的使用(山东数漫江湖)

    mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置 ...

  9. Windows下基于python3使用word2vec训练中文维基百科语料(三)

    对前两篇获取到的词向量模型进行使用: 代码如下: import gensim model = gensim.models.Word2Vec.load('wiki.zh.text.model') fla ...

  10. Metlnfo cms后台getshell漏洞复现

    整体思路 挖掘伪全局变量 然后找可控参数进行利用#伪全局变量:可理解为全局变量,例部分CMS为了全局过滤SQL注入或者XSS之类的漏洞就会将GET.POST.COOKIE等请求借入全局然后直接过滤.这 ...