Leetcode - 連結串列專題 - 234
阿新 • • 發佈:2018-12-06
請判斷一個連結串列是否為迴文連結串列。
示例 1:
輸入: 1->2 輸出: false
示例 2:
輸入: 1->2->2->1 輸出: true
進階:
你能否用 O(n) 時間複雜度和 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 """ data = [] while head: data.append(head.val) head = head.next if data == data[::-1]: return True else: return False