[leetcode: Python]234. Palindrome Linked List

来源:互联网 发布:android 7.0 源码目录 编辑:程序博客网 时间:2024/05/15 11:37

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

Follow up:
Could you do it in O(n) time and O(1) space?
思考:
是否可以在空间复杂度O(1), 时间复杂度O(n)内解决问题?

方法一:性能169ms

# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution(object):    def reverseList(self, head):        new_head = None        while head:            p = head            head = head.next            p.next = new_head            new_head = p        return new_head    def isPalindrome(self, head):        """        :type head: ListNode        :rtype: bool        """        if head == None or head.next == None:            return True        #快慢指针找中间节点        slow = fast = head        while fast.next and fast.next.next:            slow = slow.next            fast = fast.next.next        slow = slow.next        slow = self.reverseList(slow)        while head and slow:            if head.val != slow.val:                return False            else:                head = head.next                slow = slow.next        return True

方法二:性能128ms

sample 128 ms submission# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution(object):    def isPalindrome(self, head):        """        :type head: ListNode        :rtype: bool        """        rev = None        slow = fast = head        while fast and fast.next:            fast = fast.next.next            #it works because the value from right side            #is stored in memory before assignment            #so in here, it assign slow reference to rev first            #then assign the previously stored rev reference(similar to pre)            #to rev.next which is slow.next thus reversed the linked list            rev, rev.next, slow = slow, rev, slow.next        #check if it's a odd list, if yes, move slow pointer one step further        if fast:            slow = slow.next        while rev and rev.val == slow.val:            rev = rev.next            slow = slow.next        #it will return true if rev is None which means the reversed first half        #is the same as second half        return not rev

方法三:性能122ms

# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution(object):    def isPalindrome(self, head):        fast = slow = head        # find the mid node        while fast and fast.next:            fast = fast.next.next            slow = slow.next        # reverse the second half        node = None        while slow:            nxt = slow.next            slow.next = node            node = slow            slow = nxt        # compare the first and second half nodes        while node: # while node and head:            if node.val != head.val:                return False            node = node.next            head = head.next        return True

方法四:性能129ms

# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution(object):    def isPalindrome(self, head):        l1=[]        while head:            l1.append(head.val)            head=head.next        if l1==l1[::-1]:            return True        else:            return False
0 0
原创粉丝点击