链表:
判断链表是否环形、是否回文
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]
网友评论