Java鏈表ListNode的理解與操作


一:單向鏈表介紹

鏈表是一種數據結構,和數組同級。比如,Java中我們使用的ArrayList,實現原理是數組。而LinkedList的實現原理就是鏈表。在鏈表中,數據的添加和刪除都較為方便,就是在進行循環遍歷時效率不高,訪問比較耗費時間。
單向鏈表是一種線性表,實際上是由節點(Node)組成的,一個鏈表擁有不定數量的節點。其數據在內存中存儲是不連續的,它存儲的數據分散在內存中,每個結點只能也只有它能知道下一個結點的存儲位置。由N各節點(Node)組成單向鏈表,每一個Node記錄本Node的數據及下一個Node。向外暴露的只有一個頭節點(Head),我們對鏈表的所有操作,都是直接或者間接地通過其頭節點來進行的。

image

上圖中最左邊的節點即為頭結點(Head),但是添加節點的順序是從右向左的,也就是尾插。最先添加的節點(Head)對下一節點的引用可以為空。引用是引用下一個節點的地址而非下一個節點的對象。因為有着不斷的引用,所以頭節點就可以操作所有節點了。
下圖描述了單向鏈表存儲情況。存儲是分散的,每一個節點只要記錄下一節點,就把所有數據串了起來,形成了一個單向鏈表。

節點(Node)是由一個需要儲存的對象(val或者data)及對下一個節點的引用(Node.next)組成的。也就是說,節點擁有兩個成員:儲存的對象、對下一個節點的引用。下面圖是具體的說明:
image

二、因此單鏈表的結構就是這樣:

public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class MyList {
    private ListNode head = null; //頭節點

    public boolean add(int a) {  //添加新節點
        ListNode newNode = new ListNode(a);//實例化一個新節點a
        if (head == null) { //若頭節點為空,新節點賦值給頭節點
            head = newNode;
            return true;
        }
        ListNode cur = head;  //若頭節點不為空,用cur代替head進行操作。防止修改head的值
        while (cur.next != null) { //遍歷到最后一個節點
            cur = cur.next;
        }
        cur.next = newNode ; //讓上一個節點的next指向新節點,完成連接
        return true;
    }

    public boolean delete(int a){ //刪除指定節點
        if(head == null){  //若頭節點為空,返回false
            return false; 
        }
        if(head.val == a){ 
            head = head.next;
            return true;
        }
        ListNode cur = head; //用cur代替head進行操作。防止修改head的值(以下不再解釋)
        while (cur.next != null){ //遍歷所有節點
            if(cur.next.val == a){//對cur.next節點進行判斷,如果是要刪除的,直接讓cur.next指向被刪除節點的next節點。即為:cur.next = cur.next.next;
                cur.next = cur.next.next;
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public int size(){  //返回節點長度
        int flag = 0;
        ListNode cur = head;
        while(cur != null){
            flag++;
            cur =cur.next;
        }
        return flag;
    }

    public int find(int a){  //查找節點,返回下標
        ListNode cur = head;
        int flag = 0;
        while(cur.next != null){
            if(cur.val != a){
                flag++;
                cur = cur.next;
            }
            return flag;
        }
        return -1;
    }

    public ListNode get(int x){ //用下標查找節點
        if(head == null){
            return null;
        }
        ListNode cur = head;
            for (int i = 0; i < x; i++) {
                cur = cur.next;
            }
            return cur;
    }
}

鏈表的其他操作

//鏈表反轉
public ListNode reverse(ListNode head){
    ListNode cur = head;
    ListNode prev = null;
    if(head == null){
        return null;
    }
    while(cur != null) {
        ListNode tem = cur.next;
        cur.next = prev;
        prev = cur;
        cur = tem;
    }
    return prev;
}

參考來源: https://blog.csdn.net/qq_41437542/article/details/108768900


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM