Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8560841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:22:09+00:00 2026-06-11T16:22:09+00:00

Im trying to create a public bool remove(E element) method that removes the first

  • 0

Im trying to create a public bool remove(E element) method that removes the first node that includes the element and if no such a node is found it returns false otherwise true…How can I do that ? im having trouble linking the removed node’s predecessor to its successor to close the gap created by the node’s removal…this is my code so far…thank you

public class SinglyLinkedList<E> {
private final SLNode<E> head;
private final SLNode<E> tail;
int length;

// creates an empty list
public SinglyLinkedList() {
    head = new SLNode<E>();
    tail = new SLNode<E>();
    head.setSuccessor(tail);
    length = 0;
}

// adds new node on beginning of the list
public void add(E element) {
    SLNode<E> node = new SLNode<E>(element, null);
    node.setSuccessor(head.getSuccessor());
    head.setSuccessor(node);
}

// adds new node on beginning of the list
public void add(SLNode<E> node) {
    node.setSuccessor(head.getSuccessor());
    head.setSuccessor(node);
}

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    SLNode<E> cursor = head.getSuccessor();
    while (cursor != tail) {
        sb.append(cursor.getElement()).append(" ");
        cursor = cursor.getSuccessor();
    }
    sb.append("\n");
    return sb.toString();
}
 }

slnode class

public class SLNode<E> {
private E element;
private SLNode<E> successor;

public SLNode() {
    element = null;
    successor = null;
}

public SLNode(E theElement, SLNode<E> theSuccessor) {
    element = theElement;
    successor = theSuccessor;
}

public E getElement() {
    return element;
}

public void setElement(E newElement) {
    element = newElement;
}

public SLNode<E> getSuccessor() {
    return successor;
}

public void setSuccessor(SLNode<E> newSuccessor) {
    successor = newSuccessor;
}
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:22:10+00:00Added an answer on June 11, 2026 at 4:22 pm

    The trick is as you iterate, you need to keep a reference to both the previous and the current element. The reference to the previous element is what lets you close the gap:

    public boolean remove(E element) {
        SLNode<E> previous = head;
        SLNode<E> current = head.getSuccessor();
    
        while (current != tail) {
            if (current.getElement().equals(element)) {
                previous.setSuccessor(current.getSuccessor());
    
                return true;
            }
    
            previous = current;
            current = current.getSuccessor();
        }
    
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a 2 dimensional array of Node objects as follows public
I am trying to create a general method for disposing an object that implements
I'm using EF4.1 code first, I'm trying to to create a query which returns
I'm trying to create a public gist via javascript. I'm not using any authentication
InSymfony (Doctrine) I am trying to create a public function with the code below.
I am trying to create a webview in adroid with private browsing enabled. public
I am trying to create a drop down list using asp.net mvc. Model: public
I'm trying to access a static method in a Utils class I created: public
I am trying create a delegate representation of constructor by emitting a Dynamic Method,
Well im trying to create a custom container class that contains in my case

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.