[1, 2, 3, 4, 5, 6]
2
Note that 2 here represents a node numbered 2, and the node number starts from 0. Therefore, the node with number 2 is the node with val equal to 3.
Then the inlet node of the output ring is 3
**thinking** **(Linked list, fast and slow pointer scanning)** O ( n ) O(n) O(n) The method of this question is ingenious. With two pointers f i r s t , s e c o n d first,second first,second Start from the starting point, f i r s t first first One step at a time, s e c o n d second second Take two steps at a time. If in the process s e c o n d second second get to`null`,Then there is no ring. Otherwise when f i r s t first first and s e c o n d second second After meeting, let f i r s t first first Return to the starting point, s e c o n d second second Stay where you are, and then the two pointers take one step at a time. When they meet, the meeting point is the entrance of the ring. ![](https://img-blog.csdnimg.cn/2021042619071423.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYyOTI4NQ==,size_16,color_FFFFFF,t_70) Proof: as shown in the figure above, a Is the starting point, b It's the entrance to the ring, c Is the first meeting point of two pointers, ab The distance between is x,bc The distance between is y. Then when f i r s t first first get to b Due to s e c o n d second second than f i r s t first first Take twice as many roads, so s e c o n d second second Already from b Started walking on the ring x Step, may be more than 1 lap, distance b Still bad y Step (this is because the first meeting point is b after y Step, let's first return b Point, then s e c o n d second second Will retire 2 y Step, that is, distance b It's a little bad y Step); therefore s e c o n d second second from b Point go x+y Step back b A little, so s e c o n d second second from c Let's go. Let's go x You can just walk to b Point and let f i r s t first first Start from scratch, go x You can just walk there b Point. So the second meeting point is b Point. **Time complexity** f i r s t first first A total of 2 x + y 2x+y 2x+y Step, s e c o n d second second A total of 2 x + 2 y + x 2x+2y+x 2x+2y+x Step, so the two pointers go a total of 5 x + 3 y 5x+3y 5x+3y Step. Because when the first time f i r s t first first get to b At 1:00, s e c o n d second second One lap at most to catch up f i r s t first first,therefore y Less than the length of the ring, so x + y x+y x+y Less than or equal to the total length of the linked list. So the total time complexity is O ( n ) O(n) O(n). **code**
/**
-
Definition for singly-linked list.
-
struct ListNode {
-
int val;
-
ListNode *next;
-
ListNode(int x) : val(x), next(NULL) {}
-
};
*/
class Solution {
public:
ListNode *entryNodeOfLoop(ListNode *head) { if(!head || !head->next) return NULL;//If the first or second node of the linked list is empty ListNode *first = head, *second = head; while(first && second) { first = first->next; second = second->next; if(second) second = second->next; else return NULL; if(first == second) { first = head; while(first!=second) {
Spring full set of teaching materials
Spring is the "sunflower dictionary" of Java programmers, which provides various great tricks that can simplify our development and greatly improve development efficiency! At present, 99% of companies use spring. You can go to major recruitment websites. Spring is a necessary skill, so you must master it.
Data collection method: Click here to go to the blue portal
catalog:
Some contents:
Spring source code
- Part I overview of Spring
- The second part is the core idea
- The third part is the implementation of IoC and AOP (custom Spring framework)
- Part IV advanced application of Spring IOC
Basic characteristics
Advanced features - The fifth part is the in-depth analysis of Spring IOC source code
Elegant design
Design pattern
Note: principles, methods and techniques - Part VI Spring AOP application
Declare transaction control - The seventh part is the in-depth analysis of Spring AOP source code
Necessary notes, necessary drawings and easy to understand language to resolve knowledge difficulties
Scaffold framework: SpringBoot Technology
Its goal is to simplify the creation, development and deployment of Spring applications and services, simplify the configuration file, use embedded web server, contain many out of the box microservice functions, and can be jointly deployed with spring cloud.
The core idea of Spring Boot is that convention is greater than configuration. Applications only need a few configurations, which simplifies the application development mode.
- Getting started with SpringBoot
- configuration file
- journal
- Web development
- Docker
- SpringBoot and data access
- Startup configuration principle
- Custom starter
Microservice architecture: Spring Cloud Alibaba
Like Spring Cloud, Spring Cloud Alibaba is also a set of microservice solutions, including the necessary components for developing distributed application microservices, so that developers can easily use these components to develop distributed application services through the Spring Cloud programming model.
- Introduction to microservice architecture
- Introduction to Spring Cloud Alibaba
- Microservice environment construction
- Service governance
- Service fault tolerance
- Service gateway
- Link tracking
- ZipKin integration and data persistence
- Message driven
- SMS service
- Nacos config - service configuration
- Seata - distributed transactions
- Dubbo rpc communication
Spring MVC
catalog:
Some contents:
3978)]
[external chain picture transferring... (img-8F2WAezs-1629247263978)]
[external chain picture transferring... (img-4i5RkrWP-1629247263979)]
Some contents:
[external chain picture transferring... (img-86lhc8ZU-1629247263980)]
[external chain picture transferring... (img-DRIdp65R-1629247263980)]