当前位置:网站首页>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;
}
}
边栏推荐
- VPS安装Virtualmin面板
- Supercomputing simulation research has determined a safe and effective carbon capture and storage route
- Is Sanli futures safe? How to open a futures account? How to reduce the handling charge of futures at present?
- SSH principle and public key authentication
- Ternsort model integration summary
- Elevator dispatching (pairing project) ①
- QQ get group link, QR code
- Post man JSON script version conversion
- Simple understanding of seesion, cookies, tokens
- IO stream ----- open
猜你喜欢
Discussion | has large AI become autonomous? Lecun, chief scientist of openai
Elevator dispatching (pairing project) ③
JMeter assembly point technology and logic controller
Summary of Shanghai Jiaotong University postgraduate entrance examination module firewall technology
Canoe - the third simulation project - bus simulation - 2 function introduction, network topology
Attributes and methods in math library
(August 9, 2021) example exercise of air quality index calculation (I)
Application of slice
Detailed array expansion analysis --- take you step by step analysis
Deepmind proposed a Zuan AI, which specially outputs network attack language
随机推荐
Software testing related resources
R built in data set
os. Path built-in module
Performance test method
Solaris 10 network services
F12 clear the cookies of the corresponding web address
Video analysis
Canoe - the third simulation project - bus simulation-1 overview
QQ group administrators
本地Mysql忘记密码的修改方法(windows)
How to create a new virtual machine
VPS installation virtualmin panel
Function parameters (positional parameters, default value parameters, variable parameters, named keyword parameters, keyword parameters)
QQ one click cookie acquisition
Day01 preliminary packet capture
Appscan installation error: unable to install from Net runtime security policy logout appscan solution
Install freeradius3 in the latest version of openwrt
试题库管理系统–数据库设计[通俗易懂]
20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13
Reptile learning 4 winter vacation series (3)