In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Golang how to use leetcode to achieve a circular linked list, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
Given a linked list, returns the first node where the linked list begins to enter the ring. Returns null if the linked list has no loops.
To represent the rings in a given linked list, we use the integer pos to indicate where the tail of the linked list is connected to the linked list (the index starts at 0). If pos is-1, there are no rings in the linked list.
Description: modification of the given linked list is not allowed.
Example 1:
Input: head = [3, 2, 10, 10, 4], pos = 1
Output: tail connects to node index 1
Explanation: there is a ring in the linked list whose tail is connected to the second node.
Example 2:
Input: head = [1jue 2], pos = 0
Output: tail connects to node index 0
Explanation: there is a ring in the linked list whose tail is connected to the first node.
Example 3:
Input: head = [1], pos =-1
Output: no cycle
Explanation: there are no rings in the linked list.
Ideas for solving the problem:
1. First of all, use the fast and slow pointer to judge whether there is a ring.
2. Suppose the position where the fast and slow pointers meet is the p position after the starting point m of the ring, and the remaining Q positions of the ring.
Then:
A ~ (m ~ (m) p) ~ (2 *) (m ~ p)-k * (p ~ Q) k is an integer, which is related to the ratio of m length to ring size.
B, let's assume that karma is 1, massiq.
C ~ (mae) m ~ * (p ~ Q)-p = (k ~ m ~ 1) * (p ~ Q) + Q
3, it can be seen that if the slow pointer starts from the meeting position and the new pointer starts from the beginning, the meeting position must be the starting point of the ring.
/ * Definition for singly-linked list. * type ListNode struct {* Val int * Next * ListNode *} * / func detectCycle (head * ListNode) * ListNode {fast:=head slow:=head flag:=false for fastening roomnil & & fast.NextQuantification nil {fast=fast.Next.Next slow=slow.Next if fastening roomnil & & slow==fast {flag=true break}} if! flag {return nil } fast=head for fastening slowly {fast=fast.Next slow=slow.Next} return fast} will it be helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.