In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to find rings on a single linked list in Golang. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Question: an one-way linked list, how to detect whether there is a ring, and what is the initial node of the ring?
Package mainimport ("fmt") type ListNode struct {value int next * ListNode} func NewListNode (I int) * ListNode {val: = new (ListNode) val.value = i return val} func main () {A1: = NewListNode (1) a2: = NewListNode (2) a3: = NewListNode (3) a4: = NewListNode (4) a5: = NewListNode (5) / / 1 → 2 → 3 → 4 → 5 / / ↑⎽⎽⎽⌟ a1.next = a2 a2.next = a3 a3.next = a4 a4.next = a5 a5.next = a3 head: = DetectCycle (A1) fmt.Println (head.value)} func DetectCycle (head * ListNode) * ListNode {fast: = head slow: = head for {if fast.next = = nil | slow.next = = nil {break} fast = Fast.next.next slow = slow.next if fast = = slow {/ / find the meeting point of fast and slow pointers break}} if fast = = nil | | slow = = nil {return nil} / / after finding the meeting point of fast and slow pointers Move at the same speed as the pointer. Find the starting point of the ring slow = head for {if fast = = slow {break} fast = fast.next slow = slow.next} return slow}. This is the end of the article on "how to find a ring on a single linked list by Golang". I hope the above content can be helpful to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.