10、linked-list-cycle
对于一个给定的链表,返回环的入口节点,如果没有环,返回null
拓展:
你能给出不利用额外空间的解法么?
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Follow up:
Can you solve it without using extra space?
思路:借用下牛客大佬的图,再额外画蛇添足一点第一个式子详细写开是这样,时间相同两者相遇满足下列等式,设慢指针移到速度为1,则(a+b)/1 = [a+b(此时快指针移动的距离必定超过a+b)+ n*(b+c)]/2(快指针移动额外的n圈b+c与慢指针相遇),即2*(a+b)=a+b+n*(b+c)。
10、linked-list-cycle
最新推荐文章于 2021-05-07 12:06:16 发布