Write a method called deleteBack that deletes the last value (the value at the back of the list) and returns the deleted value. If the list is empty, throw a NoSuchElementException .
Add the above method to the LinkedIntList class from this chapter
Implementation of deleteBack method:
public int deleteBack()
{
if(front == null)
throw new NoSuchElementException();
int value = front.data;
if(front.next == null)
{
front = null;
}
else
{
ListNode current = front;
while(current.next.next != null)
{
value = current.data;
current = current.next;
}
current.next = null;
}
return value;
}