简体   繁体   中英

Reversing a singly linked list iteratively

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?

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

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

Works with this implementation of 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;

    }

}

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

Using a self-contained implementation, ie 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;
    }
}
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;
}
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;  
}

Modifying the Node Class

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM