Reverse Singlelink List Java

Kann mir jemand sagen, warum mein code dosent Arbeit? Ich will umkehren, eine einfach verkettete Liste in java: Dies ist die Methode (funktioniert nicht richtig)

public void reverseList(){
    Node before = null;
    Node tmp = head;
    Node next = tmp.next;
    while(tmp != null){
      if(next == null)
         return;
      tmp.next = before;
      before = tmp;
      tmp = next;
      next = next.next;
    }
}

- Und das ist die Knoten-Klasse:

public class Node{
   public int data;
   public Node next;
   public Node(int data, Node next){
      this.data = data;
      this.next = next;
   }
}

Auf Eingang 4->3->2->1 ich habe Ausgabe 4. Ich gedebuggt und es setzt Pointer korrekt, aber trotzdem habe ich nicht bekommen, warum gibt es nur 4.

InformationsquelleAutor der Frage sammy333 | 2014-03-24

Schreibe einen Kommentar