I need help removing the last index of a linked list a singular linked list . He
ID: 3833242 • Letter: I
Question
I need help removing the last index of a linked list a singular linked list .
Here is my class file and the method I am working on please help
.public class GymMembersLL {
/**************** ATTRIBUTES *******************************************
/* Here go your attributes, i.e., the information that is contained in
* your new "type"
* We can also see these new types as "blue-prints" of "things" we are
* going to build
***********************************************************************/
// Attributes ARE GIVEN TO YOU...
private GymMember mymember;
private GymMembersLL next;
/***************** METHODS *********************************************
* Note that none of the methods below are static.
***********************************************************************/
/**************** CONSTRUCTORS *****************************************
* Note that the signatures are different from those we are used to
***********************************************************************/
// Constructors
public GymMembersLL(GymMember m) {
mymember = m;
next = null;
}
public GymMembersLL(GymMember m, GymMembersLL n) {
mymember = m;
next = n;
}
/***************** SETTERS / MUTATORS **********************************
* Methods that allow to set or modify the values of the attributes
* One method per attribute
* Note that the methods are not static
***********************************************************************/
public void setMember(GymMember m) {
mymember = m;
}
public void setNext(GymMembersLL n) {
next = n;
}
/**************** GETTERS / ACCESSORS **********************************
* Methods that allow to access the values of the attributes
* One method per attribute
* Note that the methods are not static
***********************************************************************/
public GymMember getMyMember() {
return mymember;
}
public GymMembersLL getNext() {
return next;
public void removeTail() {
GymMembersLLiter =this;
if (next != null);
iter = next;
while (iter.getNext != null)
//previous = iter;
iter = iter.getNext();
Explanation / Answer
HI.. I dont have the whole code of yours So cannot run it . But I'll provide a way to delete the last node.
The algorithm use to delete the last node is as follows: -
1. Initialize pointer p to head of linked list.
2. Traverse the linked list till we found a node which follows below condition: -
node.getNext().getNext() == null
i.e. the node whose next element's next points to null i.e. second last element.
3. Set the next pointer of this node to null.
i.e. not.setNext(null);
i.e. set Next of the second last node to null i.e. now second last node becomes the last node.
Your code (method to remove last node) which I changed is as follows: -
public void removeTail() {
GymMembersLL iter = this;
while (iter.getNext().getNext() != null) // searching till second last node is found
// previous = iter;
iter.setNext(null); // set the next pointer of second last node to null
}
Hope this helped.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.