Hello, we don't need two pointers... public E removeLast() { if (tail == null) return null; E data = tail.data; if (tail == head) tail = head = null; else { Node temp = head; while (temp.next != tail) temp = temp.next; tail = temp; tail.next = null; } return data; }
Using tmp.next.next works fine as well once you're sure your list has two or more itens. What we've just done! public E removeLast() { if(head == null) return null; E data = tail.data;
Hello, we don't need two pointers...
public E removeLast() {
if (tail == null)
return null;
E data = tail.data;
if (tail == head)
tail = head = null;
else {
Node temp = head;
while (temp.next != tail)
temp = temp.next;
tail = temp;
tail.next = null;
}
return data;
}
Using tmp.next.next works fine as well once you're sure your list has two or more itens. What we've just done!
public E removeLast() {
if(head == null) return null;
E data = tail.data;
if(head == tail) {
head = tail = null;
} else {
Node tmp = head;
while(tmp.next.next != null) {
tmp = tmp.next;
}
tail = tmp;
tail.next = null;
}
currentSize--;
return data;
}
Thank you so much. Your students are so lucky to have you as their professor.
The BEST explanation of data-structures. Thx so much! Add more topics from computer-science plz :)
Can't help but like these videos because they teach me quite a lot. Professor's own habits and jokes teach me when the material fails to
the line "previous.next = tail" dont need to be "previous.next = null" ?
That confused me too!
lol he updated it at 11:26
thx for clarifying this! we teach each other
I stuck there too. Stopped the video and thought for minutes what's the purpose of that lol
All your lectures are awesome...
This's so helpful,many thanks
How about have previous and next Nodes into your node class?
tail = tail.previous;
tail.next = null;
at 9:40, what if previous.next == tail is removed? is it necessary?
I'm confused at 9:40, but at 11:22, processor found the issue when checking the end condition, so no issue now!
is...he writing in reverse?
great