当前位置:网站首页>Force buckle 142 Circular linked list II
Force buckle 142 Circular linked list II
2022-07-04 11:23:00 【Yangshiwei....】
subject :
Ideas :
Judge whether the linked list is linked , You can use the speed pointer , If the pointer traverses to null, It will not form a ring , If the two overlap , Then form a ring , There are two ways to judge the entry point ,1 When overlapping , Add a new one from head Start node , With overlapping points next, The next overlapping point is the entry point ,2 Yes, it is hash Mark the place where the fast pointer has passed .
Code :
Hash + Speed pointer :
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
HashMap<ListNode,Integer> hash=new HashMap<ListNode,Integer>();
ListNode l1=head;
ListNode l2=head;
while(l2!=null){
if(hash.containsKey(l2)){
return l2;
}else{
hash.put(l2,0);
}
l1=l1.next;
l2=l2.next;
if(l2==null){
return null;
}else{
if(hash.containsKey(l2)){
return l2;
}else{
hash.put(l2,0);
}
l2=l2.next;
}
}
return null;
}
}
Three pointers
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode l1=head;
ListNode l2=head;
while(l2!=null){
l1=l1.next;
l2=l2.next;
if(l2==null){
return null;
}else{
l2=l2.next;
if(l2==l1){
ListNode l3=head;
while(l3!=l1){
l1=l1.next;
l3=l3.next;
}
return l3;
}
}
}
return null;
}
}
边栏推荐
- Climb Phoenix Mountain on December 19, 2021
- Post man JSON script version conversion
- Software testing related resources
- Function parameters (positional parameters, default value parameters, variable parameters, named keyword parameters, keyword parameters)
- Fundamentals of database operation
- Heartbeat error attempted replay attack
- 本地Mysql忘记密码的修改方法(windows)
- Automatic translation between Chinese and English
- Fundamentals of software testing
- Installation of ES plug-in in Google browser
猜你喜欢
QQ group collection
Canoe - the third simulation project - bus simulation-1 overview
Properties and methods of OS Library
How to create a new virtual machine
Oracle11g | getting started with database. It's enough to read this 10000 word analysis
F12 clear the cookies of the corresponding web address
Detailed explanation of classic process synchronization problems
Appscan installation error: unable to install from Net runtime security policy logout appscan solution
Analysis function in SQL
2021-08-09
随机推荐
(August 10, 2021) web crawler learning - Chinese University ranking directed crawler
QQ group collection
Function parameters (positional parameters, default value parameters, variable parameters, named keyword parameters, keyword parameters)
Design and common methods of test case documents
Swagger and OpenAPI
Attributes and methods in math library
How to deal with the relationship between colleagues
Summary of Shanghai Jiaotong University postgraduate entrance examination module firewall technology
Postman advanced
Safety testing aspects
2020 Summary - Magic year, magic me
Elevator dispatching (pairing project) ④
Global function Encyclopedia
unit testing
Solaris 10网络服务
Lvs+kept realizes four layers of load and high availability
Get the data of the top 100 headlines today with Tianxing data
Data transmission in the network
Installation of ES plug-in in Google browser
Alibaba cloud server connection intranet operation