You are viewing a single comment's thread. Return to all comments →
Why must you declare a "Node node = head"? Initially I had:
if(head == null)
head = new Node();
head.data = data;
while(head.next != null) head = head.next;
head.next = new Node();
head.next.data = data;
But this method doesn't work. May you explain why? Thank you!
because the question asks to "return the head of the updated linked list". In your case you're returning the pointer (is that correct Java terminology?) to the last node.
I did the same mistake, initially and was scratching my head to figured where I went wrong. Hence people say understanding the requirement is important. You may right functionally correct code, but if that is not meeting the requirement, then the whole purpose of your program/app is defeated.