簡體   English   中英

Java:LinkedList反向

[英]Java: LinkedList Reverse

我正在嘗試實現我自己的LinkedList實現的reverse功能。 使用我的LinkedList實現:

public class LinkedList<T> {
    public Node head;

    public LinkedList(){
        // Add HEAD
        head = new Node(null);
    }

    public void add(T data){
        getLastNode().next = new Node(data);
    }

    public void insert(int index, T data){
        if(index == 0){
            throw new Error(); // TODO: What is the Error Type?
        }

        Node current = head;

        for (int i = 0; i != index - 1 ; i ++) {
            current = current.next;
            if (current == null){
                throw new IndexOutOfBoundsException();
            }
        }

        Node next = current.next;
        Node newNode = new Node(data);
        current.next = newNode;
        newNode.next = next;
    }

    public T get(int index){
        return getNode(index).data;
    }

    public void delete(int index){
        if (index == 0){
            throw new IndexOutOfBoundsException("Cannot delete HEAD node");
        }

        Node prev = getNode(index - 1);
        Node next = prev.next.next;
        prev.next = null;

        prev.next = next;
    }

    public void reverse(){ // TODO: Last node links to a null node
        Node prev = null;
        Node current = head;
        Node next = null;

        while(current != null){
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }

        head = new Node(null);
        head.next = prev;
    }

    public void display(){
        Node current = head;

        String diagram = String.format("head->");
        while(current.next != null){
            current = current.next;
            diagram += String.format("%s->", current.data);
        }
        System.out.println(diagram);
    }

    private Node getNode(int index){
        Node node = head;

        for(int i = 0; i != index; i++){
            node = node.next;
            if(node == null){
                throw new IndexOutOfBoundsException();
            }
        }

        return node;
    }

    private Node getLastNode(){
        Node current = head;

        while(current.next != null){
            current = current.next;
        }

        return current;
    }

    public class Node {
        private Node next;
        private T data;

        public Node(T data){
            this.data = data;
        }

        public Node getNext(){
            return this.next;
        }
    }
}

而這個main功能:

    LinkedList list = new LinkedList();
    list.add("e1");
    list.add("e2");
    list.add("e3");
    list.add("e4");

    list.display();
    list.reverse();
    list.display();

顯示的結果為:

head->e1->e2->e3->e4->
head->e4->e3->e2->e1->null->

這是由於e1仍連接到磁頭而發生的。 如果我使用反向實現在線提供:

    Node prev = null;
    Node current = head;
    Node next = null;

    while(current != null){
        next = current.next;
        current.next = prev;
        prev = current;
        current = next;
    }

    head = prev;

然后結果將拋棄e4: head->e3->e2->e1->null->

我在這是要干嘛? 為什么我的實現與其他人不同?

另外:為什么每個人都使用帶有head作為參數的reverse函數,如果開發人員輸入不同的節點可能會出現問題?

您將第一個節點用作列表的頭。 反向功能的解決方案是這樣的:

    head.next = prev;

您必須保留“ head”節點,但要更改其“ next”字段。

該函數的其余部分完全不變:

public void reverse(){ // TODO: Last node links to a null node
    Node prev = null;
    Node current = head.next;
    Node next = null;

    while(current != null){
        next = current.next;
        current.next = prev;
        prev = current;
        current = next;
    }

    head.next = prev;  // *** The only change ***
}

在構造函數中,您具有:

public LinkedList(){
    // Add HEAD
    head = new Node(null);
}

那么,“ head”是一個最初不指向任何內容的節點。

在反向功能中,“ head”節點不變,您無需創建另一個。 但是它必須指向正確的第一個節點。

如果列表為空,則此“頭”指向空。 如果列表中只有一個節點,則此“頭”指向它。 如果列表中有多個節點,則此“頭”必須指向最后一個節點。

因此,您需要更改其“下一個”字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM