美文网首页
02-14:leetcode重刷8之哈希与数组

02-14:leetcode重刷8之哈希与数组

作者: 是黄小胖呀 | 来源:发表于2021-02-14 23:54 被阅读0次

链表:

判断链表是否环形、是否回文

1、是否链表

# Definition for singly-linked list.

# class ListNode:

#     def __init__(self, x):

#         self.val = x

#         self.next = None

class Solution:

    def hasCycle(self, head: ListNode) -> bool:

        d={}

        while head:

            if head in d:

                return True

            else:

                d[head]=1

            head=head.next

        return False

2、是否回文

# Definition for singly-linked list.

# class ListNode:

#     def __init__(self, x):

#         self.val = x

#         self.next = None

class Solution:

    def isPalindrome(self, head: ListNode) -> bool:

       v=[]

       while head:

           v.append(head.val)

           head=head.next

       return v==v[::-1]

相关文章

网友评论

      本文标题:02-14:leetcode重刷8之哈希与数组

      本文链接:https://www.haomeiwen.com/subject/dhuyxltx.html