Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use leetcode to realize a circular linked list in golang

2025-04-09 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report