只出现一次的数字 class Solution: def singleNumber(self, nums: List[int]) -> int: return reduce(lambda x,y: x^y, nums) #XOR 环形链表 class Solution: def hasCycle(self, head: ListNode) -> bool: if not head or not head.next: return False slow=head #慢指针 fast=head.next #快指针 while slow!=fast: if not fast or not fast.next: return False #如果两个都是快的则循环不会执行 slow=slow.next fast=fast.next.next return True #快慢指针相遇返回TRUE,说明存在闭环 环形链表II class Solution: def detectCycle(self, head: ListNode) -> ListNode: if not head: return None fast,slow=head,head #快慢指针同时出发 while slow.next and fast.next and fast.next.next: slow=slow.next #慢指针每次走一步 fast=fast.next.next #快指针每次走两步 if slow==fast: #如果有环继续找入口 break else: return None slow=head #慢指针从头开始 while slow!=fast: #相遇的指针慢指针从头开始,快指针从相遇地方开始每次走一步,直到再次相遇 slow=slow.next fast=fast.next return slow