题解 | #判断一个链表是否为回文结构# 双指针求解

判断一个链表是否为回文结构

https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f

class Solution:
    def isPail(self , head: ListNode) -> bool:
        # write code here
        lst = []
        while head:
            lst.append(head.val)
            head = head.next
        n = len(lst)
        if n == 1 or n == 0:
            return True

        l, r = 0, n - 1
        while l < r:
            if lst[l] != lst[r]:
                return False
            l += 1
            r -= 1
        return True

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务