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?

先找到链表中点,将第二部分反转,然后比较两部分链表的值。

/**
* Definition for singly-linked list.
* public class ListNode {
*    int val;
*    ListNode next;
*    ListNode(int x) { val = x; }
* }
*/
public class Solution {
  public boolean isPalindrome(ListNode head) {
    if (head == null || head.next == null) {
      return true;
    }
    // Find the mid element
    ListNode fast = head;
    ListNode slow = head;
    while(fast.next != null && fast.next.next != null) {
      slow = slow.next;
      fast = fast.next.next;
    }
    ListNode head2 = slow.next;
    slow.next = null;

    // Reverse second half
    ListNode p1 = head2;
    ListNode p2 = head2.next;
    while(p2 != null && p1 != null) {
      ListNode temp = p2.next;
      p2.next = p1;
      p1 = p2;
      p2 = temp;
    }
    head2.next = null;

    // Compare two parts
    ListNode p = p2 == null? p1: p2;
    ListNode q = head;
    while (p != null) {
      if (p.val != q.val) {
        return false;
      }
      p = p.next;
      q = q.next;
    }
    return true;
  }
}

转载于:https://www.cnblogs.com/shini/p/4691066.html

234. Palindrome Linked List相关推荐

  1. 【回文串7】LeetCode 234. Palindrome Linked List

    LeetCode 234. Palindrome Linked List Solution1:我的答案 一遍过,哈哈哈! /*** Definition for singly-linked list. ...

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

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

  3. 234. Palindrome Linked List - Easy

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

  4. LeetCode 234. Palindrome Linked List

    题目: Given a singly linked list, determine if it is a palindrome. 思路: 给定一个链表,判断它是不是回文链表 根据链表的奇偶分情况,然后 ...

  5. [LeetCode] 234. Palindrome Linked List_Easy tag: 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. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...

  7. leetcode python3 简单题234. Palindrome Linked List

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十四题 (1)题目 英文: Given a singly linked li ...

  8. [swift] 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 ...

  9. 【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) t ...

最新文章

  1. Accept-Encoding
  2. SQLite剖析之异步IO模式、共享缓存模式和解锁通知
  3. android 内存检测框架,Android项目内存泄漏检测
  4. [转]细说Redis监控和告警
  5. html让时间只展示年月日_JS 如何动态显示当前年月日时分秒-百度经验
  6. python sorted怎么排序_python sorted怎么降序排序
  7. c语言怎么写注释,C语言注释
  8. Pandas 库之 DataFrame
  9. 让微软起死回生之作:CEO纳德拉18年新书《刷新》
  10. 解决QQ语音通话后耳机失效的问题
  11. 一文学会会计记账-会计科目、借贷关系和会计分录的小白理解
  12. 云计算板块-云计算基础介绍
  13. 阿里面试官鬼得很,问我为什么他们阿里要禁用Executors创建线程池?
  14. 粉笔公考——方法精讲——资料分析
  15. Spring boot 日志分档基于log4j2.yml 配置文件
  16. 中级职称计算机论文发表要求吗,中级职称论文发表的字数要求是多少?
  17. 通达OA使用手册(一)
  18. 以 “铝型材的特点” 为中心,写一段话
  19. 刚刚出新的Kubernetes 却曝出了“高危”安全漏洞;亚马逊将推免费新闻视频服务,对标苹果 | 极客头条...
  20. 精辟--中国古代商人秘而不宣的经商十诀

热门文章

  1. sam服务器是什么_使用SAM CLI将机器学习模型部署到无服务器后端
  2. 明明知道银行存款会贬值,为什么还有那么多人把钱放在银行?
  3. python判断正数和负数教案_正数和负数 教学设计
  4. dbscan算法python实现_挑子学习笔记:DBSCAN算法的python实现
  5. mysql 连续签到天数_新版签到活动明天上线,福利活动抢鲜看~
  6. 图形学教程Lecture 13: RayTracing1(Whitted-Style Ray Tracing)知识点总结
  7. 【TensorFlow-windows】name_scope与variable_scope
  8. 支付宝APP支付 统一下单 php服务端 tp5
  9. POJ2777(线段树裸题)
  10. Web测试容易忽略的地方