LeetCode Palindrome Linked List
题意:判断链表是否对称的,要求时间复杂度为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相关推荐
- [LeetCode] Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 一开始想用栈,但是试来试去发现写不出来遂放弃,后来想想再不济可以转换成数组然后 ...
- 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 ...
- LeetCode Palindrome Linked List (回文链表)
题意:给个单链表,判断是否为回文. 思路:有点BT,处理不好都是死循环.一般思路是,二分查找中心点,根据奇偶个元素,反置前半部分,再判断是否回文,再恢复前半部分. 步骤: (1)在二分查找中心点时判断 ...
- [LeetCode][JavaScript]Palindrome Linked List
Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up: Could ...
- 【回文串7】LeetCode 234. Palindrome Linked List
LeetCode 234. Palindrome Linked List Solution1:我的答案 一遍过,哈哈哈! /*** Definition for singly-linked list. ...
- LeetCode Palindrome Partitioning(dfs +回文串 )
问题:给出一个字符串,输出回文串,使得拼接后为原来的字符串 思路:先通过动态规划得到所有的回文串,然后使用深度优先搜索得到所有的解 具体代码参考: https://github.com/wuli249 ...
- 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 ...
- Leetcode 142. Linked List Cycle II
地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...
- 【To Do】LeetCode 142. Linked List Cycle II
LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...
最新文章
- StoryBoard学习(5):使用segue页面间传递数据
- 用CURD来拯救前端的一切的一切
- Linux排序命令sort笔记
- 文章页网址有.html,Phpcms V9文章内容页自定义HTML网址技巧
- Bailian3860 Bailian3724 unix纪元【日期时间】
- 阿里 虚拟主机和弹性web托管的具体区别是什么
- python汉化包放哪_python 汉化
- 从零开始学编程_数据结构
- 新cBSS敏捷发布实践
- 第八章:Java8新增特性,Lambda表达式
- js 剩余时间,包括天数,小时,分数,秒
- 第五节:通信之WLAN(MAC地址)
- 如何从0到1搭建电商促销系统
- APP性能测试--功耗测试
- oracle select into 导致报错
- C语言CRC校验码计算与校验
- Maven超细致史上最全Maven下载安装配置教学(2022更新...全版本)建议收藏...赠送IDEA配置Maven教程
- dedecms xss oday通杀所有版本 可getshell
- 蒙特卡洛方法试验的一般过程和经典例子
- Android数据库SQLite入坑指南
热门文章
- Git历险记(四)——索引与提交的幕后故事
- [LCS]半个月的成果,用RTCClient开发的Robot!
- python小项目案例-Python小项目:快速开发出一个简单的学生管理系统
- 小学生python入门-极度舒适的全套 Python 入门教程,小学生看了也能学会
- python下载地址-python下载网址
- python推荐系统-基于Python的推荐系统的设计与实现
- python编程有用吗-分享8点超级有用的Python编程建议
- python话雷达图-PYTHON绘制雷达图代码实例
- python笔记基础-Python学习笔记(基础)
- python解释器是什么-python编译器和解释器的区别是什么