Leetcode 分享筆記

234. Palindrome Linked List

題目

leetcode
b

python


class Solution(object):
    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        # 從頭開始與從尾巴開始,如果一樣代表回文。用兩個List來做比對
        curr = head
        pre = None
        head_list = []
        while curr != None:
            head_list.append(curr.val)
            next = curr.next
            curr.next = pre
            pre = curr
            curr = next
        
        first = pre
        pre_list = []
        while first != None:
            pre_list.append(first.val)
            first = first.next
                
        if(head_list == pre_list):
            return True
        else:
            return False