Palindrome Linked List

Palindrome Linked List

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

Example

Input: 1->2
Output: false

Solution

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        num_list = []
        if not head or not head.next:
            return True
        fast = slow = head
        while fast and fast.next:
            num_list.insert(0, slow.val)
            slow = slow.next
            fast = fast.next.next
        
        if fast:
            slow = slow.next
        
        for num in num_list:
            if num!=slow.val:
                return False
            slow = slow.next
        return True
上一篇:WPF 如何流畅地滚动ScrollViewer 简单实现下


下一篇:131. Palindrome Partitioning