zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    java web前端发送请求的4种方式
    简单的jQuery前端验证码校验
    验证码实现原理
    Objective-C的内存管理(一)黄金法则的理解
    UIview层次管理
    IOS设置图片背景
    Google Code Jam 2014 Round 1 A:Problem C. Proper Shuffle
    Google Code Jam 2014 Round 1 A:Problem B. Full Binary Tree
    Google Code Jam 2014 Round 1 A:Problem A Charging Chaos
    UVA 10209
  • 原文地址:https://www.cnblogs.com/rosyYY/p/10997975.html
Copyright © 2011-2022 走看看