当前位置:网站首页>[dai4] the entry node of the link in jz23 linked list
[dai4] the entry node of the link in jz23 linked list
2022-04-21 18:19:00 【strawberry47】
Title Description :

reflection :
Build a list, Storage nodes , Duplicate occurs , Just go back to .
class Solution:
def EntryNodeOfLoop(self, pHead):
if pHead is None or pHead.next is None:
return None
mem = []
while pHead not in mem:
mem.append(pHead)
pHead = pHead.next
if pHead is None:
return None
else:
return pHead
answer :
The answer is set(), Ratio of occupied memory list A lot less ; Remember to use more in the future set, It doesn't allow repetition
The space complexity required for the above answer is O ( n ) O(n) O(n), Another answer is double pointer , The space complexity is O ( 1 ) O(1) O(1)
To solve the equation ~
Assume that the outer length of the ring is a, The inner length of the ring is b; Move the pointer two spaces at a time , The slow pointer moves one grid at a time ;
- Go to the entrance quickly : f a s t = a + n ∗ b fast =a+n*b fast=a+n∗b;
- The speed pointer must meet in the ring , here f a s t − s l o w = n ∗ b fast-slow=n*b fast−slow=n∗b; Again because f a s t = 2 ∗ s l o w fast=2*slow fast=2∗slow, Substitute , When we met s l o w = n ∗ b slow=n*b slow=n∗b
- After encounter , Give Way fast Back to the head node , Slow the pointer and keep walking , When they meet again , It's the head node ; s l o w = n ∗ b + a slow=n*b+a slow=n∗b+a, It happens to meet the fast node at the entrance .

It took me a long time to understand , This is too clever

class Solution:
def EntryNodeOfLoop(self, pHead):
# write code here
slow = pHead
fast = pHead
while fast !=None and fast.next !=None:
fast = fast.next.next
slow = slow.next
if slow == fast:
while pHead != slow:
pHead =pHead.next
slow = slow.next
return slow
return None
版权声明
本文为[strawberry47]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211814289927.html
边栏推荐
猜你喜欢

Teach you to easily solve CSRF Cross Site Request Forgery Attack

C language operator summary

Advanced formula 47 of C language: recursive function analysis

Laravel soar (2. X) - automatically monitor and output SQL optimization suggestions and assist laravel to apply SQL optimization

About LINQ statements

靶机渗透练习77-DC9

靶机渗透练习76-DC8

Target penetration exercise 76-dc8

Advanced formula 48 of C language: principle of function design

After fastjson is automatically upgraded to fastjson2, the idea development environment is normally printed into a jar package and an error exception POM is reported after publishing the generated env
随机推荐
How to use serialization and deserialization of JSON in golang
Golang中Json的序列化和反序列化怎么使用
干货 | 读懂 Appium 日志,让测试效率翻倍!
教你轻松解决CSRF跨站请求伪造攻击
Target penetration exercise 80 momentum: 1
How does IOT platform realize business configuration center
Advanced formula 45 of C language: the secret of function parameters (Part 2)
中介者模式
redis启动服务和连接客户端
爬虫案例01
靶机渗透练习74-DC6
Target penetration exercise 75-dc7
干货|app自动化测试之Andriod WebView如何测试
终于有人讲明白了!原来这才是全球低时延一张网技术
不同研发协作模式在云效中的应用
Mycat水平分表(E-R表)
Teach you to easily solve CSRF Cross Site Request Forgery Attack
移植openharmony之添加wifi驱动
Failed to install network card driver (resolved)
Summary of mongodb user permissions