当前位置:网站首页>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;
}
}
边栏推荐
- Introduction to canoe automatic test system
- software test
- Canoe: what is vtsystem
- Configure SSH key to realize login free
- Xiaobing · beauty appraisal
- Canoe - the third simulation project - bus simulation-1 overview
- Canoe - the third simulation project - bus simulation - 3-1 project implementation
- First article
- Climb Phoenix Mountain on December 19, 2021
- Post man JSON script version conversion
猜你喜欢
Detailed array expansion analysis --- take you step by step analysis
(August 9, 2021) example exercise of air quality index calculation (I)
Take advantage of the world's sleeping gap to improve and surpass yourself -- get up early
Introduction to canoe automatic test system
Foreach (system.out:: println) usage
2020 Summary - Magic year, magic me
2018 meisai modeling summary +latex standard meisai template sharing
os. Path built-in module
2021-08-09
Some summaries of the 21st postgraduate entrance examination 823 of network security major of Shanghai Jiaotong University and ideas on how to prepare for the 22nd postgraduate entrance examination pr
随机推荐
守护进程Xinted和日志记录Syslogd
Getting started with window functions
Analysis function in SQL
Post man JSON script version conversion
QQ get group information
Installation of ES plug-in in Google browser
Data transmission in the network
Canoe-the second simulation project-xvehicle-1 bus database design (idea)
Install freeradius3 in the latest version of openwrt
Usage of case when then else end statement
Common tips
Elevator dispatching (pairing project) ④
array_ The contains() function uses
Digital simulation beauty match preparation -matlab basic operation No. 6
Automatic translation between Chinese and English
QQ get group link, QR code
Global function Encyclopedia
Daemon xinted and logging syslogd
Cacti主机模板之定制版
Notes on writing test points in mind mapping