Код: Выделить всё
public void reverseList() {
SinglyLinkedList reverse = new SinglyLinkedList();
Node present = head;
while(present != null) {
reverse.addFirst(present.getElement());
present = present.getNext();
}
this.head = reverse.head;
this.tail = reverse.tail;
this.size = reverse.size;
}
Код: Выделить всё
public void reverseList() {
SinglyLinkedList reverse = new SinglyLinkedList();
Node present = head;
while(present != null) {
reverse.addFirst(present.getElement());
present = present.getNext();
}
this = reverse; // I know this is syntacally wrong... but you get my point
}
Код: Выделить всё
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();
list.addFirst(10);
list.addLast(20);
list.addLast(30);
list.printList(); // Output: 10 20 30
list = list.reverseList();
list.printList(); // Output: 30 20 10
}
как указано выше. Я объяснил
Подробнее здесь: https://stackoverflow.com/questions/786 ... current-ob
Мобильная версия