国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

編程導(dǎo)航算法通關(guān)村第一關(guān)|青銅|鏈表基礎(chǔ)

這篇具有很好參考價(jià)值的文章主要介紹了編程導(dǎo)航算法通關(guān)村第一關(guān)|青銅|鏈表基礎(chǔ)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

單鏈表構(gòu)造方法

Java

JVM有棧區(qū)和堆區(qū)

棧區(qū):存引用,就是指向?qū)嶋H對(duì)象的地址。。

堆區(qū):存的是創(chuàng)建的對(duì)象。

編程導(dǎo)航算法通關(guān)村第一關(guān)|青銅|鏈表基礎(chǔ),算法,算法,鏈表,數(shù)據(jù)結(jié)構(gòu)

定義

規(guī)范的鏈表定義

public class ListNode{
    private int data;
    private ListNode next;
    public ListNode(int data){
        this.data=data;
    }
    public int getData(){
        return data;
    }
    public void setData(int data){
        this.data=data;
    }
    public ListNode getNext(){
        return next;
    }
    public void setNext(ListNode next){
        this.next=next;
    }
}

LeetCode算法題中常用

public class ListNode {
    public int val;
    public Node next;
    Node(int x) {
        val = x;
        ext = null;//作用不大,寫(xiě)了更標(biāo)準(zhǔn)
    }
}

遍歷

public static int getListLength(Node head){
    int length=0;
    Node node=head;
    while(node!=null){
        length++;
        node=node.next;
    }
    return length;
}

插入

public static Node insertNode(Node head,Node nodeInsert,int position){
    //將節(jié)點(diǎn)插入一個(gè)空鏈表
    if(head==null){
        return nodeInsert;
    }
    //越界
    int size=getLength(head);
    if(position>size+1||position<1){
        System.out.println("位置參數(shù)越界");
        return head;
    }
    //表頭插入
    if(position)==1){
        nodeInsert.next=head;
        head=nodeeInsert;
        return head;
    }
    //其他位置插入
    Node pNode=head;
    int count=1;
    while(count<position-1){
        pNode=pNode.next;
        count++;
    }
    nodeInsert.next=pNode.next;
    pNode.next=nodeinsert;
    return head;    
}

刪除

public static Node deleteNode(Node head,int position){
    //排除空鏈表
    if(head==null){
        return null;
    }
    //越界
    int size=getLength(head);
    if(position >size+1||position <1){
        System.out.println("位置參數(shù)越界");
        return head;
    } 
    //刪除節(jié)點(diǎn)
    if(position==1){
        head=head.next;
    }else{
        Node pNode=head;
        int count =1;
        while(count<position-1){
            count++;
            pNode=pNode.next;
        }
        Node delNode=pNode.next;
        pNode.next=delNode.next;
    }
    return head;
}

雙向鏈表

編程導(dǎo)航算法通關(guān)村第一關(guān)|青銅|鏈表基礎(chǔ),算法,算法,鏈表,數(shù)據(jù)結(jié)構(gòu)

Java

結(jié)點(diǎn)

class DoubleNode{
    public int data;
    public DoubleNode pre;
    public DoubleNode next;
    public DoubleNode(int data){
        this.data=data;
    }
}

結(jié)構(gòu)&遍歷

public class DoubleLinkList{
    private DoubleNode first;
    private DoubleNode last;
    public DoubleLinkList(){
        first=null;
        last=first;
    }
    //從頭遍歷
    public void displayForward(){
        DoubleNode current=first;
        while(current!=null){
            current.displayNode();
            current=current.next;
        }
    }
    //從尾部遍歷
    public void displayBackward(){
        DoubleNode current=last;
        while(current!=null){
            current.displayNode();
            current=current.pre;
        }
    }
}

插入

從頭插入

DoublyLinkList doublyLinkList = new DoublyLinkList();
doublyLinkList.insertFirst(20);
public void insertFirst(int data) {
    DoubleNode newDoubleNode = new DoubleNode(data);
    if (isEmpty()) {
        last=newDoubleNode;//列表初始化時(shí)first,last都為null
    }else{
        first.pre=newDoubleNode;
        newDoubleNode.next=first;
    }	
    first=newDoubleNode;//讓該結(jié)點(diǎn)稱(chēng)為第一個(gè)節(jié)點(diǎn)
}

從尾插入

public void insertLast(int data) {
    DoubleNode newDoubleNode = new DoubleNode(data);
    if (isEmpty()) {
        first=newDoubleNode;//列表初始化時(shí)first,last都為null
    }else{
        newDoubleNode.pre=last;
        last.next=newDoubleNode;
    }
    last=newDoubleNode;//讓該結(jié)點(diǎn)稱(chēng)為最后一個(gè)節(jié)點(diǎn)
}

從某個(gè)值為key的節(jié)點(diǎn)后面插入

public void insertAfter(int key, int data){
    DoubleNode newDoubleNode = new DoubleNode(data);
    DoubleNode current = first;
    //找值為key的節(jié)點(diǎn)
    while ((current != null) && (current.data != key)){
        current = current.next;
    }
    //若current==null,要么鏈表為空,要么沒(méi)有值為key的數(shù)
    if (current == null){
       //根據(jù)題意來(lái)寫(xiě)... 
    }else{
        //這里還有可能key值是最后一個(gè)數(shù),寫(xiě)法參考上面,這里不再考慮
        newDoubleNode.next=current.next;
        current.next.prev = newDoubleNode;
        newDoubleNode.pre=current;
        current.next=newDoubleNode;
    }
}

刪除

刪除頭結(jié)點(diǎn)

public DoubleNode deleteFirst(){
    //如果只有一個(gè)節(jié)點(diǎn)。需要多考慮一個(gè)變量last
    if (first.next == null){
        last=null;
    }else{
        first.next.pre=null;
    }
    first=first.next;  
}

刪除尾結(jié)點(diǎn)

public DoubleNode deleteLast(){
    //如果只有一個(gè)節(jié)點(diǎn)。需要多考慮一個(gè)變量first
    if (first.next == null){
        first=null;
    }else{
        last.pre.next=null;
    }
    last=last.pre;
}

按值刪除文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-608647.html

public DoubleNode deleteKey(int key){
    DoubleNode current = first;
    //尋找key值所在的節(jié)點(diǎn)
    while (current != null && current.data != key){
        current = current.next;
    }
    //若current==null,要么鏈表為空,要么沒(méi)有值為key的數(shù)
    if (current == null){
       //根據(jù)題意來(lái)寫(xiě)...
        return null;
    }else{
        //這里還有可能key值是第一個(gè)或者最后一個(gè)數(shù),寫(xiě)法參考上面,這里不再考慮 
        current.next.prev = current.pre;
        current.pre.next=current.next;
    }
}

到了這里,關(guān)于編程導(dǎo)航算法通關(guān)村第一關(guān)|青銅|鏈表基礎(chǔ)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 算法通關(guān)村第一關(guān)-鏈表青銅挑戰(zhàn)筆記

    算法通關(guān)村第一關(guān)-鏈表青銅挑戰(zhàn)筆記

    平時(shí)我們一般都是用數(shù)組,每個(gè)數(shù)組都會(huì)有一個(gè)相對(duì)應(yīng)的索引,這樣就使得數(shù)組能夠方便的調(diào)用出對(duì)應(yīng)索引得到需要的數(shù)據(jù),但是這也造成了一個(gè)問(wèn)題,那就是不好在數(shù)組中插入或者刪除一個(gè)數(shù)據(jù),例如 int arr[]={1,2,4,5}如果我想在數(shù)組中的2和4中添加一個(gè)數(shù)據(jù)3 那么我們首先就

    2024年02月15日
    瀏覽(26)
  • 算法通關(guān)村第一關(guān)——鏈表青銅挑戰(zhàn)筆記

    算法通關(guān)村第一關(guān)——鏈表青銅挑戰(zhàn)筆記

    鏈表的基本單元就是 節(jié)點(diǎn) ,也就是說(shuō)鏈表是由一個(gè)一個(gè)節(jié)點(diǎn)構(gòu)成的。 而對(duì)于節(jié)點(diǎn)來(lái)說(shuō),里面至少會(huì)包含一個(gè) 指針 和一個(gè) 數(shù)據(jù)元素 ,也就是如下圖所示: 其中數(shù)據(jù)域用來(lái)存放數(shù)據(jù)元素,指針域用來(lái)存放指向下一個(gè)節(jié)點(diǎn)的指針,這樣一個(gè)一個(gè)連接起來(lái)的就是鏈表。如下圖所

    2024年02月16日
    瀏覽(27)
  • [Go版]算法通關(guān)村第一關(guān)青銅——鏈表青銅挑戰(zhàn)筆記

    [Go版]算法通關(guān)村第一關(guān)青銅——鏈表青銅挑戰(zhàn)筆記

    單向鏈表圖示: 雙向鏈表圖示: 環(huán)形單向鏈表圖示: 環(huán)形雙向鏈表圖示: 源碼地址: GitHub-golang版本 如果是單向的,需要將當(dāng)前節(jié)點(diǎn) 定位到要插入節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn) ,否則一旦過(guò)了將無(wú)法回頭找到前一個(gè)節(jié)點(diǎn) 如果是雙向的,將當(dāng)前節(jié)點(diǎn) 定位到要插入節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)、

    2024年02月13日
    瀏覽(21)
  • 編程導(dǎo)航算法通關(guān)村第 1 關(guān)|青銅 - C++是如何構(gòu)造出鏈表的

    編程導(dǎo)航算法通關(guān)村第 1 關(guān)|青銅 - C++是如何構(gòu)造出鏈表的

    ?????????在C++中,鏈表是由一系列節(jié)點(diǎn)構(gòu)成的,每個(gè)節(jié)點(diǎn)包含一個(gè)值和一個(gè)指向下一個(gè)節(jié)點(diǎn)的指針。 ? ? ? ? 我們可以用結(jié)構(gòu)體定義出一個(gè)節(jié)點(diǎn): ???????? ? ? ?在定義完后,我們將鏈表進(jìn)行初始化,并插入5條數(shù)據(jù): ? 接著我們?cè)谥骱瘮?shù)中進(jìn)行測(cè)試,看看是否能成

    2024年02月13日
    瀏覽(19)
  • 算法通關(guān)村第一關(guān)——鏈表青銅挑戰(zhàn)筆記(單鏈表)

    在LeeCode中一般這樣創(chuàng)建鏈表 要注意創(chuàng)建一個(gè)變量來(lái)遍歷,不要把head丟掉了 count position - 1可以方便操作,還能防止下標(biāo)越界(cur為null)

    2024年02月15日
    瀏覽(21)
  • 【無(wú)標(biāo)題】算法通關(guān)村第一關(guān)——鏈表青銅挑戰(zhàn)筆記

    【無(wú)標(biāo)題】算法通關(guān)村第一關(guān)——鏈表青銅挑戰(zhàn)筆記

    算法通關(guān)村第一關(guān)——鏈表青銅挑戰(zhàn)筆記 C語(yǔ)言是如何構(gòu)造出鏈表的 0.定義節(jié)點(diǎn)結(jié)構(gòu) 1.建立頭指針 2.建立temp指針 3.將節(jié)點(diǎn)連起來(lái) 3.1 把p指向temp 3.2 設(shè)立循環(huán)節(jié)點(diǎn)a+temp指向a+temp變?yōu)閍

    2024年02月15日
    瀏覽(25)
  • [Go版]算法通關(guān)村第一關(guān)——鏈表青銅挑戰(zhàn)筆記

    [Go版]算法通關(guān)村第一關(guān)——鏈表青銅挑戰(zhàn)筆記

    單向鏈表圖示: 雙向鏈表圖示: 環(huán)形單向鏈表圖示: 環(huán)形雙向鏈表圖示: 源碼地址: GitHub-golang版本 如果是單向的,需要將當(dāng)前節(jié)點(diǎn) 定位到要插入節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn) ,否則一旦過(guò)了將無(wú)法回頭找到前一個(gè)節(jié)點(diǎn) 如果是雙向的,將當(dāng)前節(jié)點(diǎn) 定位到要插入節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)、

    2024年02月15日
    瀏覽(21)
  • 編程導(dǎo)航算法第一關(guān) |鏈表的基礎(chǔ)

    今天也是算法通關(guān)村剛開(kāi)始,學(xué)習(xí)了鏈表。 首先,鏈表是一種最基本的結(jié)構(gòu),我們通常在收到一個(gè)鏈表的時(shí)候只有一個(gè)指向鏈表頭的指針head,因?yàn)殒湵砭褪峭ㄟ^(guò)頭指針來(lái)尋找其他結(jié)點(diǎn)的。 鏈表中的每個(gè)結(jié)點(diǎn)都存在它的數(shù)據(jù)和指向下一個(gè)節(jié)點(diǎn)的指針。 在遍歷鏈表中,我們只需

    2024年02月15日
    瀏覽(17)
  • 編程導(dǎo)航算法通關(guān)村第1關(guān) | 青銅:?jiǎn)捂湵淼脑鰟h改查

    鏈表中的每一個(gè)節(jié)點(diǎn)包含數(shù)據(jù)域和指針域 (構(gòu)造在一個(gè) struct 中),數(shù)據(jù)變量存儲(chǔ)數(shù)據(jù),指針變量存儲(chǔ)下一個(gè)節(jié)點(diǎn)的地址。 鏈表的一個(gè)節(jié)點(diǎn)只能有一個(gè)后繼 特點(diǎn): 鏈表節(jié)點(diǎn)在內(nèi)存中的位置可以不連續(xù)。 思路: 用 struct 包裝節(jié)點(diǎn) :成員包括數(shù)據(jù)和地址 用 class 包裝鏈表 :成員有

    2024年02月13日
    瀏覽(18)
  • 編程導(dǎo)航算法通關(guān)村 | 鏈表中環(huán)的問(wèn)題

    編程導(dǎo)航算法通關(guān)村 | 鏈表中環(huán)的問(wèn)題

    在鏈表中還有一類(lèi)經(jīng)典的問(wèn)題,那就是判斷鏈表中是否有環(huán),如果有環(huán),環(huán)的入口如何確定。 用Hash來(lái)做會(huì)很容易,遍歷一遍,把每一個(gè)元素都放入map中,有環(huán)的話(huà),那我們就一定能匹配到。 這里最重要的問(wèn)題, 如果快的能到達(dá)表尾就不會(huì)有環(huán),否則如果存在環(huán),則慢指針一

    2024年02月14日
    瀏覽(24)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包