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 ...
随机推荐
- eclipses配置tomcat
1,项目右键属性,设置为1.8,与jdk相对应 2,自动发布,tomcat 3,使用自己的tomcat 4,
- HTTPS中CA证书的签发及使用过程
1,HTTPS 简单来讲,HTTPS (Secure Hypertext Transfer Protocol)安全超文本传输协议就是安全的HTTP,我们知道HTTP是运行在TCP层之上的,HTTPS在 ...
- C# 过滤字典中的数据 并将过滤后的数据转成新的字典对象
Dictionary<string, object> dic = new Dictionary<string, object>(); dic.Add("); dic. ...
- 无Xwindow的linux系统安装VMware Tools
首先登陆linux 系统,最好是root用户: 然后挂载cdrom: mkdir -p /mnt/cdrom mount -t iso9660 /dev/cdrom /mnt/cdrom 进入cdro ...
- Mac下安装svn服务器
本文转载自http://www.cnblogs.com/czq1989/p/4913692.html Mac默认已经安装了svn,我们只需要进行配置并开启就可以了 首先我们可以验证一下是否安装了svn ...
- IO模型(epoll)--详解-02
写在前面 从事服务端开发,少不了要接触网络编程.epoll作为linux下高性能网络服务器的必备技术至关重要,大部分游戏服务器都使用到这一多路复用技术.文章核心思想是:要让读者清晰明白EPOLL为什么 ...
- 标准C语言(3)
操作符用来描述对数字的处理规则根据操作符所需要配合的数字个数把操作符分为单目操作符,双目操作符和三目操作符 C语言里用+,-,*和/表示加减乘除四则运算,它们都是双目操作符,如果参与除法计算的两个数字 ...
- 【CF 718C】fibonacci
题意 给你一个长度为 \(n\) 的序列 \(a\),有 \(m\) 次操作,操作分两种 \(\text{1}\space \text{l}\space \text{r}\space \text{x} ...
- dlopen 加载so库
#include <stdio.h> #include <dlfcn.h> int main(int argc, char **argv) { void *handle; do ...
- k8sConfigMap资源
ConfigMap对象用于为容器中的应用提供配置数据以定制程序的行为,不过敏感的配置信息,例如密钥.证书等通常由Secret对象来进行配置.他们将相应的配置信息保存于对象中,而后在pod资源上以存储卷 ...