LeetCode中如何检测单链表是否形成环形?
- 内容介绍
- 相关推荐
本文共计133个文字,预计阅读时间需要1分钟。
pythondef get_start_of_loop(head): if not head: return None
slow=head fast=head
while fast and fast.next: slow=slow.next fast=fast.next.next
if slow==fast: break else: return No loop found
slow=head while slow !=fast: slow=slow.next fast=fast.next
return slow
***给定一个链表的头节点head,返回链表开始入环的第一个节点。如果链表无环,则返回 leetcode单链表环形链表2 ***给定一个链表的头节点head,返回链表开始入环的第一个节点。如果链表无环,则返回本文共计133个文字,预计阅读时间需要1分钟。
pythondef get_start_of_loop(head): if not head: return None
slow=head fast=head
while fast and fast.next: slow=slow.next fast=fast.next.next
if slow==fast: break else: return No loop found
slow=head while slow !=fast: slow=slow.next fast=fast.next
return slow
***给定一个链表的头节点head,返回链表开始入环的第一个节点。如果链表无环,则返回 leetcode单链表环形链表2 ***给定一个链表的头节点head,返回链表开始入环的第一个节点。如果链表无环,则返回
