data structures and algorithms in java Question 1 (4 marks) Consider the followi
ID: 3711308 • Letter: D
Question
data structures and algorithms in java
Question 1 (4 marks) Consider the following two classes Node and singlyLinkedtist: private class Node private int element private Node next; public Node (int e, Node n) element e next = n; public int getBlement return element: ) public Node getNext () public void setNext (Node n) { next = n;} return next; ) public class SinglyLinkedList private Node head# null; private Node tail#null; private int size 0 Add the method removeLast () that removes the last element from a list to the SinglyLinkedList class.Explanation / Answer
public void removeLast() { if(head != null) { if(head.getNext() == null) { head = null; tail = null; } else { Node temp = head; while (temp.getNext() != tail) { temp = temp.getNext(); } temp.setNext(null); tail = temp; } size--; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.