leetcode-easy-listnode-234 Palindrome Linked List
mycode 89.42%
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
total = 0
p1 = p2 = head
while p1:
total += 1
p1 = p1.next
if total < 2:
return True
half = total // 2 - 1
while half :
p2 = p2.next
half -= 1
if total % 2 == 1:
part_2 = p2.next.next
else:
part_2 = p2.next
p2.next , last = None , None while part_2:
new_head = part_2.next
part_2.next = last
last = part_2
part_2 = new_head while head:
if not last or head.val != last.val:
return False
head , last= head.next , last.next
return True
参考
1、使用快慢指针,凡是用了额外空间
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
if not head or not head.next:
return True new_list = [] # 快慢指针法找链表的中点
slow = fast = head
while fast and fast.next:
new_list.insert(0, slow.val)
slow = slow.next
fast = fast.next.next if fast: # 链表有奇数个节点
slow = slow.next for val in new_list:
if val != slow.val:
return False
slow = slow.next
return True
2、使用快慢指针找重点,其他思路和我相同
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if not head or not head.next:
return True # 快慢指针法找链表的中点
slow = fast = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next slow = slow.next # slow指向链表的后半段
slow = self.reverseList(slow) while slow:
if head.val != slow.val:
return False
slow = slow.next
head = head.next
return True def reverseList(self, head):
new_head = None
while head:
p = head
head = head.next
p.next = new_head
new_head = p
return new_head
leetcode-easy-listnode-234 Palindrome Linked List的更多相关文章
- <LeetCode OJ> 234. Palindrome Linked List
Total Accepted: 40445 Total Submissions: 148124 Difficulty: Easy Given a singly linked list, determi ...
- 【leetcode❤python】 234. Palindrome Linked List
#-*- coding: UTF-8 -*-class Solution(object): def isPalindrome(self, head): ""&q ...
- 【easy】234. Palindrome Linked List
ques: 判断一个链表是否回文 Could you do it in O(n) time and O(1) space? method:先将链表分为两部分,将后半部分反转,最后从前往后判断是否相等. ...
- 234. Palindrome Linked List【easy】
234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...
- 【leetcode】234. Palindrome Linked List
234. Palindrome Linked List 1. 使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点.如果是偶数个数 ...
- 234. Palindrome Linked List - LeetCode
Question 234. Palindrome Linked List Solution 题目大意:给一个链表,判断是该链表中的元素组成的串是否回文 思路:遍历链表添加到一个list中,再遍历lis ...
- 【LeetCode】234. Palindrome Linked List (2 solutions)
Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could ...
- (easy)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] 234. Palindrome Linked List (easy)
原题 回文 水题 function ListNode(val) { this.val = val; this.next = null; } /** * @param {ListNode} head * ...
- [LeetCode] 234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
随机推荐
- mybatis-generator遇到到的问题
1.Unknown system variable 'query_cache_size' https://blog.csdn.net/qq_21870555/article/details/80711 ...
- 多线程的些许理解(平台x86,具体考虑linux,windows)
多线程的些许理解 一.体系架构 1.原子操作 1) 定义 不可中断的一个或者一系列操作,也就是不会被线程调度机制打断的操作,在运行期间不会有任何的上下文切换(context switch). 2) 我 ...
- nodejs在Mac下的卸载
卸载: 在 node 官网上下载的安装包,用安装包安装的node.应该可以用一下命令行卸载: 在终端输入以下命令: sudo rm -rf /usr/local/{bin/{node,npm},lib ...
- ftp上传下载功能实现
该程序分为客户端和服务端,目前已经实现以下功能: 1. 多用户同时登陆 2. 用户登陆,加密认证 3. 上传/下载文件,保证文件一致性 4. 传输过程中现实进度条 5. 不同用户家目录不同,且只能访问 ...
- nginx搭建及加固
系统使用的是centos7 Nginx安装及配置 Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务 安装 我是用的环境是ce ...
- Zen Cart 常用SQL命令
网店日常维护过程中,难免要跟各种批量操作打交道, 虽然大多数操作可以通过批量上传 (easy populate) 和 快速更新 模块来完成, 但在应用便捷性和一些功能性的调整上, 总有一定的局限性; ...
- Mysql 查看连接数,状态及最大并发数(转载)
-- show variables like '%max_connections%'; 查看最大连接数 set global max_connections=1000 重新设置 mysql> ...
- 使用jvisualvm远程监控tomcat(阿里云ECS)
写在前面: 使用jvisualvm远程监控tomcat(阿里云ECS),连接是报错:service:jmx:rmi:////jndi/rmi:IP:端口// 连接到 IP:端口,网上找了很多资料, ...
- electron仿制qq(2) 主界面制作
制作从头开始 最后会将写好的组件放到一起的!之前写了好几天的纯css 有点累 本章中将使用sass 如果代码太长 会分两个或多个章节写代码中会有详细的注释 以便于大家阅读and理解界面可能会有部分偏差 ...
- 微信小程序轮播图的制作与跳转
<!--轮播图 --> <view class='swiperBanner'> <swiper indicator-dots='{{indicatorDots}}' au ...