チェーンテーブルが返信pythonで実装されているかどうかを確認します.


# -*- coding:utf-8 -*-

'''
 

 , 。

 ListNode* pHead, bool, 。
 :

{1,2,3,2,1}

 :true

{1,2,3,2,3}

 :false

@author chimuuu
'''

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Palindrome:
    def isPalindrome(self, pHead):
        # write code here
        if pHead == None or pHead.next == None:
            return True

        pslow = pHead
        pfast = pHead
        stack = [pslow.val]
        #  
        while True:
            #  , 
            if not pfast.next:
                mid = pslow
                break
            #  , 
            elif pfast and not pfast.next.next:
                mid = pslow.next
                break
            pslow = pslow.next
            pfast = pfast.next.next
            stack.append(pslow.val)


        #  
        print stack
        while stack and mid.next:
            tmp = stack.pop()
            if mid.val != tmp:
                return False

            print mid.val, tmp
            mid = mid.next
        return True

node1 = ListNode(1)
node2 = ListNode(2)
node3 = ListNode(3)
# node4 = ListNode(3)
node5 = ListNode(2)
node6 = ListNode(1)
node1.next = node2
node2.next = node3
node3.next = node5
# node4.next = node5
node5.next = node6


S = Palindrome()
p = S.isPalindrome(node1)
print(p)