單鏈表複習

class Node {
    public int data;
    public Node next;

    public Node() {
    }

    public Node(int data) {
        this.data = data;
    }
}
/*
 * 單鏈表  我們先構造節點
 * */

class LinkedList {
    /*
     * 頭插法
     * */
    //創建頭
    public Node head;

    public void addFirst(int data) {
        Node node = new Node(data);
        if (this.head == null) {
            this.head = node;
        } else {
            node.next = this.head;
            this.head = node;
        }
    }

    /*
     * 尾插法
     * */
    public void addLast(int data) {
        Node node = new Node(data);
        if (this.head == null) {
            this.head = node;
        } else {
            Node cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    /*
     * 任一點進行插入
     * 1.查找前驅
     * 2.判斷下標是否合法
     * 3.長度
     * 4.addIndex
     *
     * */
    public Node searchIndex(int index) {
        int count = 0;
        Node cur = this.head;
        while (count < index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    public void checkIndex(int index) {
        if (index < 0 || index > getLength()) {
            throw new
                    IndexOutOfBoundsException("下標不合法");
        }
    }

    private int getLength() {
        int count = 0;
        Node cur = this.head;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    public boolean addIndex(int index, int data) {
        checkIndex(index);
        if (index == 0) {
            addFirst(data);
            return true;
        }
        Node cur = searchIndex(index);
        Node node = new Node(data);
        node.next = cur.next;
        cur.next = node;
        return true;
    }

    public void display() {
        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //單鏈表刪除所有的節點分爲兩種1.頭結點的話直接將頭後移即可
    //2.我們是需要找到要刪除節點的前驅然後和現在要刪除的節點
// 然後只需要將要刪除的節點next,賦值給前驅即可
    /*
     * 刪除一個任意的節點key
     * key在節點中就是我們的data
     * */
    public void remove(int key) {
        //考慮我們刪除的是頭結點
        if (this.head.data == key) {
            this.head = this.head.next;
        }
        //不是頭結點情況   我們需要先找前驅
        Node prev = searchPrev(key);
        if (prev == null) {
            return;
        }
        Node del = prev.next;
        prev.next = del.next;
    }

    private Node searchPrev(int key) {
        Node prev = this.head;
        while (prev.next != null) {
            if (prev.next.data == key) {
                return prev;
            }
            prev = prev.next;
        }
        return null;
    }

    /*
     * 刪除所有key的節點
     * 循環體的條件是cur!=null
     * 我們是爲了保證下標不越界
     * 然後用prev.next來一直判斷cur的data
     * */
    public void removeAll(int key) {
        Node prev = this.head;
        Node cur = this.head.next;
        while (cur != null) {
            if (prev.next.data == key) {
                //刪除1223中的2    我們在刪除第一個2 是不能讓我們的prev 離開1
                // 所以是需要將我們的cur往後移重複遍歷
                prev.next = cur.next;
                cur = prev.next;
            } else {
                prev = cur;
                cur = prev.next;
            }
        }
        if (this.head.data == key) {
            this.head = this.head.next;
        }
    }

    public void display1(Node newHead) {
        Node cur = newHead;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public Node findMidNode() {
        Node fast = this.head;
        Node slow = this.head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    /*
     * 查找倒數第K個
     * 節點
     * 這個和我們查找中間值是一樣的原理
     * 查找倒數第幾個  我們就定義兩個A,B都爲頭的節點,
     * A先提前走k步後,然後A,B同時走,等A走到頭,B所在的位置就是我們查找的倒數第k個
     * */
    public Node findKthToTail(int k) {
        if (k <= 0) {
            return null;
        }
        Node fast = this.head;
        Node slow = this.head;
        while (k - 1 > 0) {
            if (fast.next != null) {
                fast = fast.next;
                k--;
            } else {
                System.out.println("沒有此節點");
                return null;
            }
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    /*
     * 求單鏈表的長度
     * */
    public int size(Node head) {
        int size = 0;
        Node cur = head;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    /*
     * 刪除聯繫出現的重複的節點
     * */
    public Node deleteDuplication() {
        Node newHead = new Node(-1);
        Node tem = newHead;
        Node cur = this.head;
        while (cur != null) {
            if (cur.next != null && cur.data == cur.next.data) {
                while (cur.next != null && cur.data == cur.next.data) {
                    cur = cur.next;
                }
                cur = cur.next;
                tem.next = cur;
            } else {
                tem.next = cur;
                tem = tem.next;
                cur = cur.next;
            }
        }
        return newHead;
    }

    /*
     * 反轉單鏈表
     * */
    public Node reversList() {
        Node prev = null;
        Node cur = this.head;
        Node newHe1ad = null;
        while (cur != null) {
            Node curNext = cur.next;
            //是最後才走到我們下一個節點完後會走到這個判斷條件
            if (curNext == null) {
                newHe1ad = cur;
            }
            cur.next = prev;//將第一個next置爲null
            prev = cur;
            cur = curNext;
        }
        return newHe1ad;
    }

在這裏插入圖片描述

/*
    * 基準:
    * 指定一個數,左邊都是比它小的 右邊都是比它大的
    * Node curNext=cur.next;
       cur.next=null;
     這段代碼的表達的是,我們需要將每一個節點拆開,但是我們需要先記錄下來我們的
    下一個節點地址,否則我們無法完成遍歷,也無法將每一個節點拆開。
    * */
    public Node partition(int x) {
        Node beforeStart = null;
        Node beforeEnd = null;
        Node afterStart = null;
        Node afterEnd = null;
        Node cur = this.head;
        while (cur != null) {
            Node curNext = cur.next;
            cur.next = null;
            if (cur.data < x) {
                if (beforeStart == null) {
                    beforeStart = cur;
                    beforeEnd = cur;
                } else {
                    beforeEnd.next = cur;
                    beforeEnd = cur;
                }
            } else {
                if (afterStart == null) {
                    afterStart = cur;
                    afterEnd = cur;
                } else {
                    afterEnd.next = cur;
                    afterEnd = cur;
                }
            }
            cur = curNext;
        }
        if (beforeStart == null) {
            return afterStart;
        }
        beforeEnd.next = afterStart;
        return beforeStart;
    }

在這裏插入圖片描述

/*
       迴文結構
    *1.定義兩個引用,目的:找到單鏈表的中間位置
    * 2.進行翻轉,翻轉的是後半部分
    * 3.一個head從頭開始走,slow從尾巴開始走
    * 4.只要發現對應的data不相同,呢麼就不是迴文
    *
    * */
    public boolean chkPalindrome() {
        if (this.head == null) {
            return false;
        }
        if (this.head.next == null) {
            return true;
        }
        Node fast = this.head;
        Node slow = this.head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        Node p = slow.next;
        while (p != null) {
            Node pNext = p.next;
            p.next = slow;
            slow = p;
            p = pNext;
            if (p != null) {
                pNext = p.next;
            }
        }
        while (this.head != slow) {
            if (this.head.data != slow.data) {
                return false;
            }
            if (this.head.next == slow) {
                return true;
            }
            this.head = this.head.next;
            slow = slow.next;
        }
        return true;
    }

在這裏插入圖片描述

/*
     * 1.創建一個環
     * 2.判斷是否有環
     * 3.我們找到環的入口點
     * */
    public void createCycle() {
        Node cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = this.head.next.next;
    }

    public boolean hasCycle() {
        Node fast = this.head;
        Node slow = this.head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }

    public Node detectCycle() {
        Node fast = this.head;
        Node slow = this.head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return null;
        }
        fast = this.head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章