Elenco collegato Concetti fondamentali [chiusi
-
05-11-2019 - |
Domanda
Sto cercando di capire le basi dell'elenco collegato. La definizione della mia classe Linkedlist è la seguente:
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
Ora sto affrontando un problema. Il mio codice è il seguente:
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy;
ListNode slow = head;
head.next = null;
prev.next = slow;
ListNode temp = slow.next;
prev.next = temp;
System.out.println(dummy.next); //comes out null
Perché sta uscendo come null? Dummy.next indicava la testa e ho cambiato solo lento e prev?
Possiamo usare la testa e la testa in modo intercambiabile? Se sì, allora perché accade?
// head points to a Linked list starting from 1 in 1 -> 2 -> 3
ListNode curr = head;
while(curr.next!= null){
curr= curr.next;
}
System.out.println(curr);
System.out.println(head); //these are different and head does not change
Nessuna soluzione corretta
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a cs.stackexchange