mycode   89.42%

  1. # Definition for singly-linked list.
  2. # class ListNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6.  
  7. class Solution(object):
  8. def isPalindrome(self, head):
  9. """
  10. :type head: ListNode
  11. :rtype: bool
  12. """
  13. total = 0
  14. p1 = p2 = head
  15. while p1:
  16. total += 1
  17. p1 = p1.next
  18. if total < 2:
  19. return True
  20. half = total // 2 - 1
  21. while half :
  22. p2 = p2.next
  23. half -= 1
  24. if total % 2 == 1:
  25. part_2 = p2.next.next
  26. else:
  27. part_2 = p2.next
  28. p2.next , last = None , None
  29.  
  30. while part_2:
  31. new_head = part_2.next
  32. part_2.next = last
  33. last = part_2
  34. part_2 = new_head
  35.  
  36. while head:
  37. if not last or head.val != last.val:
  38. return False
  39. head , last= head.next , last.next
  40. return True

参考

1、使用快慢指针,凡是用了额外空间

  1. class Solution:
  2. def isPalindrome(self, head: ListNode) -> bool:
  3. if not head or not head.next:
  4. return True
  5.  
  6. new_list = []
  7.  
  8. # 快慢指针法找链表的中点
  9. slow = fast = head
  10. while fast and fast.next:
  11. new_list.insert(0, slow.val)
  12. slow = slow.next
  13. fast = fast.next.next
  14.  
  15. if fast: # 链表有奇数个节点
  16. slow = slow.next
  17.  
  18. for val in new_list:
  19. if val != slow.val:
  20. return False
  21. slow = slow.next
  22. return True

2、使用快慢指针找重点,其他思路和我相同

  1. # Definition for singly-linked list.
  2. # class ListNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6.  
  7. class Solution(object):
  8. def isPalindrome(self, head):
  9. """
  10. :type head: ListNode
  11. :rtype: bool
  12. """
  13. if not head or not head.next:
  14. return True
  15.  
  16. # 快慢指针法找链表的中点
  17. slow = fast = head
  18. while fast.next and fast.next.next:
  19. slow = slow.next
  20. fast = fast.next.next
  21.  
  22. slow = slow.next # slow指向链表的后半段
  23. slow = self.reverseList(slow)
  24.  
  25. while slow:
  26. if head.val != slow.val:
  27. return False
  28. slow = slow.next
  29. head = head.next
  30. return True
  31.  
  32. def reverseList(self, head):
  33. new_head = None
  34. while head:
  35. p = head
  36. head = head.next
  37. p.next = new_head
  38. new_head = p
  39. 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. eclipses配置tomcat

    1,项目右键属性,设置为1.8,与jdk相对应 2,自动发布,tomcat 3,使用自己的tomcat 4,

  2. HTTPS中CA证书的签发及使用过程

    1,HTTPS 简单来讲,HTTPS (Secure Hypertext Transfer Protocol)安全超文本传输协议就是安全的HTTP,我们知道HTTP是运行在TCP层之上的,HTTPS在 ...

  3. C# 过滤字典中的数据 并将过滤后的数据转成新的字典对象

    Dictionary<string, object> dic = new Dictionary<string, object>(); dic.Add("); dic. ...

  4. 无Xwindow的linux系统安装VMware Tools

    首先登陆linux 系统,最好是root用户: 然后挂载cdrom: mkdir -p /mnt/cdrom mount -t iso9660 /dev/cdrom /mnt/cdrom 进入cdro ...

  5. Mac下安装svn服务器

    本文转载自http://www.cnblogs.com/czq1989/p/4913692.html Mac默认已经安装了svn,我们只需要进行配置并开启就可以了 首先我们可以验证一下是否安装了svn ...

  6. IO模型(epoll)--详解-02

    写在前面 从事服务端开发,少不了要接触网络编程.epoll作为linux下高性能网络服务器的必备技术至关重要,大部分游戏服务器都使用到这一多路复用技术.文章核心思想是:要让读者清晰明白EPOLL为什么 ...

  7. 标准C语言(3)

    操作符用来描述对数字的处理规则根据操作符所需要配合的数字个数把操作符分为单目操作符,双目操作符和三目操作符 C语言里用+,-,*和/表示加减乘除四则运算,它们都是双目操作符,如果参与除法计算的两个数字 ...

  8. 【CF 718C】fibonacci

    题意 给你一个长度为 \(n\) 的序列 \(a\),有 \(m\) 次操作,操作分两种 \(\text{1}\space \text{l}\space \text{r}\space \text{x} ...

  9. dlopen 加载so库

    #include <stdio.h> #include <dlfcn.h> int main(int argc, char **argv) { void *handle; do ...

  10. k8sConfigMap资源

    ConfigMap对象用于为容器中的应用提供配置数据以定制程序的行为,不过敏感的配置信息,例如密钥.证书等通常由Secret对象来进行配置.他们将相应的配置信息保存于对象中,而后在pod资源上以存储卷 ...