java 迭代地反转单链表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12943720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:52:46  来源:igfitidea点击:

Reversing a singly linked list iteratively

javalinked-listsingly-linked-list

提问by RonJRH

Has to be O(n) and in-place (space complexity of 1). The code below does work, but is there a simpler or better way?

必须是 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;
}

回答by RonJRH

Edited to remove the extra comparison per iteration:

编辑以删除每次迭代的额外比较:

    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;
        }
    }

回答by jn1kk

Works with this implementation of LinkedList: https://stackoverflow.com/a/25311/234307

适用于 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;

    }

}

回答by DNA

Using a self-contained implementation, i.e. the List is represented by the head Node:

使用自包含实现,即列表由头节点表示:

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;
    }
}

回答by Ted Hopp

How about this:

这个怎么样:

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;
        }
    }
}

回答by edst

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;
}

回答by user2848321

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;  
}

回答by karansingh1487

Modifying the Node Class

修改节点类

u can override toString method in Node class to input any data

你可以覆盖 Node 类中的 toString 方法来输入任何数据

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

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