Issue
I want to save the final value on a LinkedList of 5000000 integers by using an iterator. For this assignment, I need to traverse through the entire list (I know this is not the most efficient way). Here is the code that I have:
//method 1:
ListIterator ls = list.listIterator();
while (ls.hasNext()) {
var save = ls.next(); //my attempt to save the final value
ls.next();
}
What is the best way for me to save the value at the last index to a variable?
Solution
ListIterator<Integer> ls = list.listIterator();
int last = -1;
while (ls.hasNext()) {
last = ls.next();
}
Answered By - Louis Wasserman
Answer Checked By - Senaida (JavaFixing Volunteer)