1

O(n) でインプレース (スペースの複雑さ 1) である必要があります。以下のコードは機能しますが、もっと簡単で良い方法はありますか?

public void invert() {
    if (this.getHead() == null)
        return;
    if (this.getHead().getNext() == null)
        return;
    //this method should reverse the order of this linked list in O(n) time
    Node<E> prevNode = this.getHead().getNext();
    Node<E> nextNode = this.getHead().getNext().getNext();
    prevNode.setNext(this.getHead());
    this.getHead().setNext(nextNode);
    nextNode = nextNode.getNext();

    while (this.getHead().getNext() != null)
    {
        this.getHead().getNext().setNext(prevNode);
        prevNode = this.getHead().getNext();
        this.getHead().setNext(nextNode);
        if (nextNode != null)
            nextNode = nextNode.getNext();
    }
    this.head = prevNode;
}
4

7 に答える 7

11

反復ごとの余分な比較を削除するように編集:

    public void invert() {
        Node<E> prev = null, next = null;;
        if (head == null) return;
        while (true) {
            next = head.getNext();
            head.setNext(prev);
            prev = head;
            if (next == null) return;
            head = next;
        }
    }
于 2012-10-17T22:11:03.670 に答える
1

LinkedList のこの実装で動作します: https://stackoverflow.com/a/25311/234307

public void reverse() {

    Link previous = first;
    Link currentLink = first.nextLink;
    first.nextLink = null;

    while(currentLink != null) {        

        Link realNextLink = currentLink.nextLink;
        currentLink.nextLink = previous;                        
        previous = currentLink; 
        first = currentLink;    
        currentLink = realNextLink;

    }

}
于 2012-10-17T22:02:29.677 に答える
0

自己完結型の実装を使用します。つまり、リストはヘッドノードで表されます。

public class Node<E>
{
    Node<E> next;
    E value;

    public Node(E value, Node<E> next)
    {
        this.value = value;
        this.next = next;
    }

    public Node<E> reverse()
    {
        Node<E> head = null;
        Node<E> current = this;
        while (current != null) {
            Node<E> save = current;
            current = current.next;
            save.next = head;
            head = save;
        }
        return head;
    }
}
于 2012-10-17T22:08:30.240 に答える
0
public void invert() {  
  if (first == null) return;  
  Node<E> prev = null;  
  for ( Node<E> next = first.next; next != null; next = first.next) {  
    first.next = prev;  
    prev = first;  
    first = next;  
  }  
  first.next = prev;  
}
于 2013-10-05T16:59:07.353 に答える
0
public void reverse(){

    Node middle = head;
    Node last = head;
    Node first = null;

    while(last != null){

        middle = last;
        last = last.next;
        middle.next = first;
        first = middle;
    }

    head = middle;
}
于 2013-01-16T06:01:02.720 に答える
0

これはどう:

public void invert() {
    if (head != null) {
        for (Node<E> tail = head.getNext(); tail != null; ) {
            Node<E> nextTail = tail.getNext();
            tail.setNext(head);
            head = tail;
            tail = nextTail;
        }
    }
}
于 2012-10-17T21:43:46.870 に答える
-1

ノード クラスの変更

NodeクラスのtoStringメソッドをオーバーライドして、任意のデータを入力できます

public class Node { public Node node; private int data; Node(int data) { this.data = data; } @Override public String toString() { return this.data+""; }

于 2014-03-21T06:23:27.293 に答える