Delete last link of a Linked List
  • node
  • linkedlist
  • stackqueuelinkedlist
  • delete
  •   
  • Posted: 7 years ago
  • Edit
  • answers (1)
  • views (2955)

Write a function that deletes and returns the last link of a Linked List?


Posted Answers

public Link deleteLast(){

/* If the list is empty, function returns null */
if(first == null)
return null;

else{
Link current = first;
Link previous = null;
while(current.next! = null){
previous = current;
current = current.next;
}

/* If the list has one element, update 'first' */
if(previous == null)
first = null;
else
previous.next = null;
}

return current;
}

You need to Sign In to post your solution.