leetcode-python-回文链表

1)用栈,再利用字符串的思路二分对比(或者利用出栈.pop()),双指针也行。(时间空间都效率太低)

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def isPalindrome(self, head: ListNode) -> bool:
        stack = []
        while head:
            stack.append(head)
            head = head.next
        
        for i in range(len(stack)//2):
            if stack[i].val != stack[-i-1].val:
                return False
        return True

 

posted @ 2021-06-03 15:20  泊鸽  阅读(66)  评论(0)    收藏  举报