欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Java怎么確定一個鏈表有環(huán)及入口節(jié)點

這篇文章主要介紹“Java怎么確定一個鏈表有環(huán)及入口節(jié)點”,在日常操作中,相信很多人在Java怎么確定一個鏈表有環(huán)及入口節(jié)點問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java怎么確定一個鏈表有環(huán)及入口節(jié)點”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、溫嶺ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的溫嶺網(wǎng)站制作公司

如何確定一個鏈表有環(huán),入口節(jié)點是什么?

1.首先定義一個單鏈表;

var ,next,是單鏈表中的屬性,分別表示節(jié)點值和下一個節(jié)點的指向; 代碼如下:

//定義一個鏈表
  class  List{
    public  int var;
    public  List next;
//有參構(gòu)造
    public List(int var) {
        this.var = var;
    }
//無參構(gòu)造
    public List() {

    }
    //創(chuàng)建一個帶環(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)

如果存在,則返回這個節(jié)點,如果不存在則返回null,定義快慢指針,如果快的追上了慢的指針,那么這個鏈表必存在環(huán),如果沒有追上,或者都為null,那么這個鏈表沒有環(huán); 代碼如下:

//判斷是否有環(huán),并找到相遇的節(jié)點
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,確定是一個有環(huán)的鏈表;
        }
        slow = slow.next;
        fast = fast.next;
        if(fast!=null){
            fast = fast.next;
        }
    }
   return null;
}

3.尋找入口節(jié)點

先讓快指針先走環(huán)的節(jié)點的個數(shù)步,在讓慢指針開始走,如果兩個指針相遇的話,那么相遇的節(jié)點必然是環(huán)的入口節(jié)點 代碼如下:

  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ù)測試;

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怎么確定一個鏈表有環(huán)及入口節(jié)點 

到此,關(guān)于“Java怎么確定一個鏈表有環(huán)及入口節(jié)點”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

新聞標(biāo)題:Java怎么確定一個鏈表有環(huán)及入口節(jié)點
當(dāng)前鏈接:http://chinadenli.net/article22/jhpsjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名定制網(wǎng)站搜索引擎優(yōu)化手機網(wǎng)站建設(shè)Google商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化