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的更多相关文章

  1. &lt;LeetCode OJ&gt; 234. Palindrome Linked List

    Total Accepted: 40445 Total Submissions: 148124 Difficulty: Easy Given a singly linked list, determi ...

  2. 【leetcode❤python】 234. Palindrome Linked List

    #-*- coding: UTF-8 -*-class Solution(object):    def isPalindrome(self, head):        ""&q ...

  3. 【easy】234. Palindrome Linked List

    ques: 判断一个链表是否回文 Could you do it in O(n) time and O(1) space? method:先将链表分为两部分,将后半部分反转,最后从前往后判断是否相等. ...

  4. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

  5. 【leetcode】234. Palindrome Linked List

    234. Palindrome Linked List 1. 使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点.如果是偶数个数 ...

  6. 234. Palindrome Linked List - LeetCode

    Question 234. Palindrome Linked List Solution 题目大意:给一个链表,判断是该链表中的元素组成的串是否回文 思路:遍历链表添加到一个list中,再遍历lis ...

  7. 【LeetCode】234. Palindrome Linked List (2 solutions)

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

  8. (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 ...

  9. [leetcode] 234. Palindrome Linked List (easy)

    原题 回文 水题 function ListNode(val) { this.val = val; this.next = null; } /** * @param {ListNode} head * ...

  10. [LeetCode] 234. Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

随机推荐

  1. 【Lucene】小谈lucene的BooleanQuery查询对象

    BooleanQuery用于逻辑查询,即所谓的组合查询,具体的逻辑关系如下: 一个具体的使用测试,如下:

  2. Vue-cli脚手架起步

    1.安装node.js 下载地址:http://nodejs.cn/download/ 测试是否安装成功 node -V 检测安装包 npm -v 检测npm 2.安装webpack npm inst ...

  3. Django框架——基础之模型系统(ORM相关操作)

    ------------恢复内容开始------------ 1.必定会的十三条! 1.1记忆方法一:(按字母顺序记忆)   <1> all(): 查询所有结果 <2> cou ...

  4. uploadify 上传文件插件

    今天在项目中要用到文件上传功能时,想借助Jquery方式来实现,于是想到用uploadify插件来实现.不经意间在网上看到了一遍关于这个插件的用法,写的很好.在这里就分享给大家,希望对大家有帮助.以下 ...

  5. mybatis查询返回的对象不为null,但是属性值为null

    返回的对象不为null,但是属性值为null 代码如下: <resultMap id="BaseResultMap" type="com.trhui.ebook.d ...

  6. 如何使用sqlalchemy根据数据库里面的表反推出模型,然后进行查询

    关于sqlalchemy映射数据库里面的表,一般情况下我们是需要定义一个模型来映射数据库里面的表的.但是很多时候数据库里面的表都是定义好的,而且字段很多,那么有没有不定义模型,还能使用orm语法查找数 ...

  7. python-迭代器与生成器2

    python-迭代器与生成器2 def fib(max): n,a,b=0,0,1 while n<max: #print(b) yield b a,b=b,a+b #t=(b,a+b) 是一个 ...

  8. mysql 在gtid环境下缺少一部分binlog部署从库

    我的环境: 有两台linux服务器 一台是192.168.1.1  mysql  端口3301 一台是192.168.1.2   mysql 端口3303 要讨论如何恢复从库,我们得先来了解如下一些概 ...

  9. 利用java8新特性,用简洁高效的代码来实现一些数据处理

    定义1个Apple对象: public class Apple {    private Integer id;    private String name;    private BigDecim ...

  10. (转)window.parent和window.opener区别

    下面一段代码是关于window.parent和window.opener区别 来讲的,我们如果要用到iframe的值传到另一框架就要用到window.opener.document.getElemen ...