当前位置:网站首页>Leetcode palindrome linked list
Leetcode palindrome linked list
2022-07-21 00:23:00 【qq_ forty-three million four hundred and eighteen thousand eigh】
Leetcode Palindrome list
Give you the head node of a single linked list head , Please judge whether the linked list is a palindrome linked list . If it is , return true ; otherwise , return false .
Example 1:
Input :head = [1,2,2,1]
Output :true
Example 2:
Input :head = [1,2]
Output :false
Tips :
The number of nodes in the linked list is in the range [1, 105] Inside
0 <= Node.val <= 9
author : Power button (LeetCode)
link :https://leetcode.cn/leetbook/read/top-interview-questions-easy/xnv1oc/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
Stack First in, then out
The first reaction when you see the palindrome substring is the stack . First, put all elements on the stack . Then compare the stack with the original linked list nodes in turn , For the same True, The difference is False.
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
""" Stack """
stack = list() # Create a stack
h = head
while h:
stack.append(h) # All linked list elements are put on the stack
h = h.next
n1 = head
while stack:
n2 = stack.pop() # Out of the stack
if n1.val != n2.val:
return False
n1 = n1.next
return True
You can see that it takes a long time .
边栏推荐
- Joseph Ring problem
- Two duplicate operations with different functions in blender
- 什么是复制冲突
- [nepctf2022] recurrence
- What is replication conflict
- Implementation of imx8mp kdump function
- 关于 AuthorizingRealm无法注入service
- cpolar应用实例之助力航运客户远程办公
- El table uses formatter to convert strings of multiple numeric types returned by the interface into corresponding Chinese characters
- 快速入门Linxu笔记
猜你喜欢
[e-commerce operation] try these five personalized marketing methods to bid farewell to ineffective marketing!
The idea version of postman has been released, and its functions are really powerful
CDH cluster construction (6.3)
嗶哩嗶哩 直播分區頁面 自動檢索紅包直播間並跳轉
Implementation of imx8mp kdump function
Surprise! Nearly 2million depositors' deposits were stolen by "face swiping". Look at your bank card. Are there these three kinds of risks?
BiliBili live broadcast partition page automatically retrieves the red envelope live broadcast room and jumps to it
Dest0g3 520迎新赛web
Lua代码添加注释规则
NFT in the eyes of blackash: the platform is crying for slaughter, and users send money to the door
随机推荐
微信小程序开发学习4(视图与逻辑)
What is replication conflict
URL design and data source of SAP Spartacus product details page
Overwintering samples of game companies: has going to sea and boutique become a new growth point?
DBeaver的操作日志
组件化架构项目搭建——Gradle统一、依赖管理与配置
If paging by frame fails - solution
开发者必读:2022年移动应用运营增长洞察白皮书
COLA 4.0 - DDD项目实践
Using GRE to realize the communication between two internal networks across the public network
2022杭电 中超
Application of GIS technology in the medical industry: using slice map publishing technology to solve the online browsing of dmetrix digital pathological slices
实施MES系统有何难点,以及怎么解决?
若依框架分页失效---解决方案
科研丨Overleaf打开LaTeX编译报错:PDF渲染错误【已解决】
[try to hack] SQL injection less7 (into outfile and Boolean blind annotation)
医学 性别、年龄、受教育年限的校正 方法与代码
method
测试/开发程序员小张相亲记......
数据仓库中的元数据管理!