java如何確定一個(gè)鏈表有環(huán)及入口節(jié)點(diǎn)
如何確定一個(gè)鏈表有環(huán),入口節(jié)點(diǎn)是什么?
1.首先定義一個(gè)單鏈表;
var ,next,是單鏈表中的屬性,分別表示節(jié)點(diǎn)值和下一個(gè)節(jié)點(diǎn)的指向;
代碼如下:
//定義一個(gè)鏈表
class List{
public int var;
public List next;
//有參構(gòu)造
public List(int var) {
this.var = var;
}
//無參構(gòu)造
public List() {
}
//創(chuàng)建一個(gè)帶環(huán)的鏈表
public List Create(){
List a = new List(1);
List b = new List(2);
List c = new List(3);
List d = new List(4);
List e = new List(5);
List f = new List(6);
a.next = b;
b.next =c;
c.next = d;
d.next =e;
e.next = f;
f.next =d;
return a;
}
2.編寫判斷是否存在環(huán)
如果存在,則返回這個(gè)節(jié)點(diǎn),如果不存在則返回null,定義快慢指針,如果快的追上了慢的指針,那么這個(gè)鏈表必存在環(huán),如果沒有追上,或者都為null,那么這個(gè)鏈表沒有環(huán);
代碼如下:
//判斷是否有環(huán),并找到相遇的節(jié)點(diǎn)
public List MeetingNode(List node){
List slow = new List();
List fast = new List();
if(node==null) return null;
slow = node.next;
if(slow==null) return null;
fast=slow.next;
while (fast!=null && slow!=null){
if (fast==slow){
return fast; //fast追上了slow,確定是一個(gè)有環(huán)的鏈表;
}
slow = slow.next;
fast = fast.next;
if(fast!=null){
fast = fast.next;
}
}
return null;
}
3.尋找入口節(jié)點(diǎn)
先讓快指針先走環(huán)的節(jié)點(diǎn)的個(gè)數(shù)步,在讓慢指針開始走,如果兩個(gè)指針相遇的話,那么相遇的節(jié)點(diǎn)必然是環(huán)的入口節(jié)點(diǎn)
代碼如下:
public List Enterdear(List node){
if(node==null) return null;
if(MeetingNode(node)==null) return null;
int count =1;
List res2;
List res1 = MeetingNode(node);
while (res1.next!=MeetingNode(node)){
res1 = res1.next;
count++;
}
res1 = node;
for(int i = 0;i<count;i++){
res1 =res1.next;
}
res2 = node;
while (res1!=res2 && res1!=null && res2!=null){
res1 = res1.next;
res2 = res2.next;
}
return res1;
}
}
main函數(shù)測(cè)試;
ublic class Deom {
public static void main(String[] args) {
List SB = new List();
List res = SB.Create();
List dear= SB.Enterdear(res);
System.out.println(dear.var);
}
}

以上就是java如何確定一個(gè)鏈表有環(huán)及入口節(jié)點(diǎn)的詳細(xì)內(nèi)容,更多關(guān)于java鏈表及入口節(jié)點(diǎn)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
利用Spring Session和redis對(duì)Session進(jìn)行共享詳解
這篇文章主要給大家介紹了關(guān)于利用Spring、Session和redis對(duì)Session進(jìn)行共享的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
centos7如何通過systemctl啟動(dòng)springboot服務(wù)代替java -jar方式啟動(dòng)
這篇文章主要介紹了centos7如何通過systemctl啟動(dòng)springboot服務(wù)代替java -jar方式啟動(dòng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01
SpringBoot集成Prometheus實(shí)現(xiàn)監(jiān)控的過程
這篇文章主要介紹了SpringBoot集成Prometheus實(shí)現(xiàn)監(jiān)控,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
如何用ObjectMapper將復(fù)雜Map轉(zhuǎn)換為實(shí)體類
這篇文章主要介紹了如何用ObjectMapper將復(fù)雜Map轉(zhuǎn)換為實(shí)體類的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring+MyBatis實(shí)現(xiàn)數(shù)據(jù)讀寫分離的實(shí)例代碼
本篇文章主要介紹了Spring+MyBatis實(shí)現(xiàn)數(shù)據(jù)讀寫分離的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
關(guān)于Java如何用好線程池的方法分享(建議收藏)
這篇文章主要來和大家分享幾個(gè)關(guān)于Java如何用好線程池的建議,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下2023-06-06

