[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
Example 2:
Input: 1->2->2->1 Output: true
Follow up:
Could you do it in O(n) time and O(1) space?
这个题目就是先找中点,然后将中点后的list都reverse,进而分别将每个点比较,一旦不等,那么返回False,最后返回True。
T: O(n), S: O(1)
code
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def isPalindrome(self, head: ListNode) -> bool:if not head or not head.next: return Truedummy = ListNode(0)dummy.next = headslow, fast = dummy, dummywhile fast:if not fast.next:breakif not fast.next.next:fast.next = fast.next.nextslow = slow.nextfast = fast.next.nextpre, cur, count = slow, slow.next, 0while cur:count += 1temp = cur.nextcur.next = prepre = curcur = temp# compare two listsfor _ in range(count):if pre.val != head.val:return Falsepre = pre.nexthead = head.nextreturn True
转载于:https://www.cnblogs.com/Johnsonxiong/p/10817515.html
[LeetCode] 234. Palindrome Linked List_Easy tag: Linked List相关推荐
- 【回文串7】LeetCode 234. Palindrome Linked List
LeetCode 234. Palindrome Linked List Solution1:我的答案 一遍过,哈哈哈! /*** Definition for singly-linked list. ...
- [LeetCode] 234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- LeetCode 234. Palindrome Linked List
题目: Given a singly linked list, determine if it is a palindrome. 思路: 给定一个链表,判断它是不是回文链表 根据链表的奇偶分情况,然后 ...
- LeetCode 234 Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...
- [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 ...
- [LeetCode] 143. Reorder List_Middle tag: Linked List
Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You may not mo ...
- [LeetCode] 93. Restore IP Addresses_Medium tag: backtracking
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- 【整数转字符串】LeetCode 9. Palindrome Number
LeetCode 9. Palindrome Number Solution1: 不利用字符串 class Solution { public:bool isPalindrome(int x) {if ...
- 【To Understand!回文串8 哈希表】LeetCode 336. Palindrome Pairs
LeetCode 336. Palindrome Pairs Solution1:我的答案 最笨的方法,果然因为时间复杂度太高而无法AC-- 难怪是hard 只是记录一下-- class Soluti ...
最新文章
- linux(armv7/8)下gdb的安装及查看方法
- CSS3透明属性opacity
- Qt中Ui名字空间以及setupUi函数的原理和实现
- 简评《疯狂的石头》《超人归来 / Superman Returns》《谍中谍 3 / Mission Impossible 3》...
- 160 - 32 genocide1
- 经典面试题:如何保证缓存与数据库的双写一致性?
- java 最快平衡几个值_Java 集合框架面试问题集锦
- r语言library什么意思_医学统计与R语言:百分条图与雷达图
- 欲取代硬盘?SSD固态存储器前景分析
- 会话技术(Cookie Session)
- H5炫酷特效系列2——canvas特效-炫酷的心
- BZOJ 1639: [Usaco2007 Mar]Monthly Expense 月度开支( 二分答案 )
- IIS7下MP4视频无法加载解决方法
- NYOJ509-因子和阶乘
- 汇编实例2——循环累加
- 《九阴真经:iOS黑客攻防秘籍》新书发布
- mysql 索引原理详解
- jQuery的选择器大全
- 音频标准AES的一点理解
- Java集成ElasticSearch及配置类工具类整理
热门文章
- python内置函数 enumerate()的用法
- 浮点类型误区 “!=”
- MongoDB高级——聚合
- php怎么给span赋值,php给一组指定关键词添加span标签的方法
- Incorporating Lexical Priors into Topic Models(即交互式主题模型的应用)论文阅读
- 一张图看懂开源许可协议,开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别【转载】
- hog与fhog(转载)
- Ubuntu下挂载NTFS分区错误修复
- 设计模式学习笔记——组合(Composite)模式
- Git常用命令——分支操作