Mamy prostą listę:
class Node {
Node next;
int value;
...
}
oraz dostępną w niej metodę:
Node reverse(Node list) {
// if head is null or only one node, it's reverse of itself.
if ( (list==null) || (list.next == null) ) return head;
// reverse the sub-list leaving the head node.
Node reversed = reverse(list.next);
// head.next still points to the last element of reversed sub-list.
// so move the head to end.
llist.next.next = list;
// point last node to nil, (get rid of cycles)
list.next = null;
return reversed;
}
Dla listy [1, 2]
...
metoda reverse
wywoływana jest na list.next
czyli [2]
... następnie wywoływane są jakieś dziwne operacje mianowicie:
// so move the head to end.
llist.next.next = list;
// point last node to nil, (get rid of cycles)
list.next = null;
następnie metoda zwraca:
return reversed;
Moje pytanie... jak to możliwe, że
// so move the head to end.
llist.next.next = list;
// point last node to nil, (get rid of cycles)
list.next = null;
mają jakikolwiek wpływ na zmienną reversed? Przecież jest ona ustalona linijkę wcześniej i zwracana na końcu...
Z góry dzięki i pozdro.