题意:判断链表是否对称的,要求时间复杂度为O(n),空间复杂度为O(1)

思路:先计算链表结点数,然后将前半链表反转,然后再比较将半部和后半部是否相等

代码如下:

class ListNode
{int val;ListNode next;ListNode(int x) { val = x;}
}class Solution
{public boolean isPalindrome(ListNode head){int nodeCount = 0;ListNode p = head;while (p != null){nodeCount++;p = p.next;}ListNode current = head;ListNode pre = null;ListNode tmp = null;for (int i = 0; i < nodeCount / 2; i++){tmp = current.next;current.next = pre;pre = current;current = tmp;}if (1 == nodeCount % 2) current = current.next;for (int i = 0; i < nodeCount / 2; i++){if (pre.val != current.val) return false;else{pre = pre.next;current = current.next;}}return true;}
}

LeetCode Palindrome Linked List相关推荐

  1. [LeetCode] Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. 一开始想用栈,但是试来试去发现写不出来遂放弃,后来想想再不济可以转换成数组然后 ...

  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 Palindrome Linked List (回文链表)

    题意:给个单链表,判断是否为回文. 思路:有点BT,处理不好都是死循环.一般思路是,二分查找中心点,根据奇偶个元素,反置前半部分,再判断是否回文,再恢复前半部分. 步骤: (1)在二分查找中心点时判断 ...

  4. [LeetCode][JavaScript]Palindrome Linked List

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

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

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

  6. LeetCode Palindrome Partitioning(dfs +回文串 )

    问题:给出一个字符串,输出回文串,使得拼接后为原来的字符串 思路:先通过动态规划得到所有的回文串,然后使用深度优先搜索得到所有的解 具体代码参考: https://github.com/wuli249 ...

  7. Leetcode 142 Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  8. Leetcode 142. Linked List Cycle II

    地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...

  9. 【To Do】LeetCode 142. Linked List Cycle II

    LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...

最新文章

  1. StoryBoard学习(5):使用segue页面间传递数据
  2. 用CURD来拯救前端的一切的一切
  3. Linux排序命令sort笔记
  4. 文章页网址有.html,Phpcms V9文章内容页自定义HTML网址技巧
  5. Bailian3860 Bailian3724 unix纪元【日期时间】
  6. 阿里 虚拟主机和弹性web托管的具体区别是什么
  7. python汉化包放哪_python 汉化
  8. 从零开始学编程_数据结构
  9. 新cBSS敏捷发布实践
  10. 第八章:Java8新增特性,Lambda表达式
  11. js 剩余时间,包括天数,小时,分数,秒
  12. 第五节:通信之WLAN(MAC地址)
  13. 如何从0到1搭建电商促销系统
  14. APP性能测试--功耗测试
  15. oracle select into 导致报错
  16. C语言CRC校验码计算与校验
  17. Maven超细致史上最全Maven下载安装配置教学(2022更新...全版本)建议收藏...赠送IDEA配置Maven教程
  18. dedecms xss oday通杀所有版本 可getshell
  19. 蒙特卡洛方法试验的一般过程和经典例子
  20. Android数据库SQLite入坑指南

热门文章

  1. Git历险记(四)——索引与提交的幕后故事
  2. [LCS]半个月的成果,用RTCClient开发的Robot!
  3. python小项目案例-Python小项目:快速开发出一个简单的学生管理系统
  4. 小学生python入门-极度舒适的全套 Python 入门教程,小学生看了也能学会
  5. python下载地址-python下载网址
  6. python推荐系统-基于Python的推荐系统的设计与实现
  7. python编程有用吗-分享8点超级有用的Python编程建议
  8. python话雷达图-PYTHON绘制雷达图代码实例
  9. python笔记基础-Python学习笔记(基础)
  10. python解释器是什么-python编译器和解释器的区别是什么