当前位置:网站首页>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;
}
}
边栏推荐
- Login operation (for user name and password)
- Discussion | has large AI become autonomous? Lecun, chief scientist of openai
- Usage of case when then else end statement
- Definition and method of string
- 2021 annual summary - it seems that I have done everything except studying hard
- QQ group administrators
- Serialization oriented - pickle library, JSON Library
- 本地Mysql忘记密码的修改方法(windows)[通俗易懂]
- Canoe - description of common database attributes
- No response after heartbeat startup
猜你喜欢
Day01 preliminary packet capture
Foreach (system.out:: println) usage
JMeter common configuration components and parameterization
How to create a new virtual machine
20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13
JMeter assembly point technology and logic controller
Send a request using paste raw text
Reptile learning winter vacation series (2)
Performance test method
Open the neural network "black box"! Unveil the mystery of machine learning system with natural language
随机推荐
Reptile learning 4 winter vacation series (3)
2020 Summary - Magic year, magic me
Heartbeat启动后无反应
Installation of ES plug-in in Google browser
iptables导致Heartbeat脑裂
regular expression
Canoe: the fourth simulation project -- bug debugging experience
Deepmind proposed a Zuan AI, which specially outputs network attack language
Heartbeat error attempted replay attack
2018 meisai modeling summary +latex standard meisai template sharing
Fundamentals of software testing
QQ set group information
Login operation (for user name and password)
Ten key performance indicators of software applications
JMeter correlation technology
Capl: timer event
Canoe - the third simulation project - bus simulation - 2 function introduction, network topology
QQ group collection
Performance test process
First article