234. palindrome linked List [easy] (Python) __python

Source: Internet
Author: User
Topic link

The original title of https://leetcode.com/problems/palindrome-linked-list/

Given a singly linked list, determine if it is a palindrome.

Follow up:
Could do it in O (n) Time and O (1) spaces? Title Translation

Given a single linked list, to determine whether it is "palindrome linked list."
Further: Can you complete the problem in time complexity O (n) and Space complexity O (1). method of Thinking

From the angle of solving this problem, the method should be quite many, but it is necessary to eliminate some methods to meet the requirement of time and space complexity of the topic.
But it doesn't necessarily need to meet the complexity requirements for AC. So the following article, I will explain the way I know, the following code can be AC (efficiency is not guaranteed). idea of a

Considering that the single linked list is missing from the forward information, it is easy to judge a palindrome if bidirectional information can be obtained. It is considered that the node value of the single linked list is recorded in an array to judge whether the array is palindrome or not, or the single link list is expanded into a two-way list by one traversal, and then the palindrome is judged.
The following code is used to determine the array.

Code (Time O (n), space O (n))

# 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

        tmp_list = [] While head
        :
            tmp_list.append (Head.val) Head
            = head.next

        length = Len (tmp_list)
        for I in range (0, LENGTH/2):
            if tmp_list[i]!= Tmp_list[leng TH-I-1]: Return
                False return
        True
Idea two

Palindrome is mainly the first half and the second half of the comparison, if the first half of the stack, and then out of the stack and the second half of the comparison, it can be judged whether a palindrome.

Code (Time O (n), Space O (N/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

        new_list = []

        # Fast-pointer method to find the midpoint of the list
        slow = Speed = Head
        WH Ile Fast and Fast.next:
            new_list.insert (0, slow.val)
            slow = slow.next
            fast = Fast.next.next

        if fast : # The list has an odd number of nodes
            slow = Slow.next for

        val in new_list:
            if Val!= slow.val: Return
                False
            slow = SLOW.N Ext return
        True
idea three

Similar thinking two, to judge a palindrome is mainly the first half and the second half of the comparison, if the latter half of the reverse (still a single linked list), you can easily judge a palindrome.
There are many ways to realize this idea, and there are also differences in efficiency. The requirement of space complexity O (1) can be realized without the use of extra space to reverse the second half of the single linked list.

Code (Time O (n), Space O (1))

# 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, or not Head.next:return True
            # Fast and Slow pointer method to find the midpoint of the list slow = Speed = Fast.next and Fast.next.next:slow = Slow.next Fast = Fast.next.next slow = slow.next # Slow point to the second half of the list slow = Self.reverselist (slow) whi  Le 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 

Description
The function Reverselist () method given above is only one way to reverse the linked list, and the Space complexity O (1). For more information on how to reverse a single linked list, you can refer to:
206. Reverse linked List [easy] (Python)

PS: Novice Brush Leetcode, new handwritten blog, write wrong or write not clear also please help point out, thank you.
reprint Please specify: http://blog.csdn.net/coder_orz/article/details/51306985

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.