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 8925483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:42:29+00:00 2026-06-15T07:42:29+00:00

Good evening I’m trying to implement a single linked list by myself and I’ve

  • 0

Good evening

I’m trying to implement a single linked list by myself and I’ve run into an issue when I want to create a search method. Evidently when you want to search for a node (which will be used to insert a node at a certain place) you will have to evaluate some values to see if you reached the right spot. Considering my nodes only have a data field as a identifier, I don’t see any other way than using that. However, since the data field isn’t unique there might be multiple nodes eligible.

Consider the following list: 5, 7, 2, 8, 3, 1, 6, 5, 8, 4, 2.
When I want to add a node somewhere in the list (say: After the node with value 8) he will go trough the list and add the new node after the first occurrence of ‘8’. What should I do if I wanted to insert it after the 2nd 8?

Is this even possible with a Single Linked List?

Other than that I’d like to have some feedback on my ‘removeLast()’ method which doesn’t seem to do what I want it to do (remove the last node from the list). I am aware my code isn’t supposed to work if the list has only 1 value, I’ll look into that as soon as the general code of removing the last node works.

My code can be found here.

Edited with code:

 public class SingleLinkedList {

 public void deleteLast() {
    if (lastNode != null) {
        Node currentNode = firstNode;

        while (currentNode != null) {
            Node nextNode = currentNode.getNextNode();
            Node nextNextNode = nextNode.getNextNode();

            if (nextNextNode == null) {
                nextNextNode = null;
                lastNode = nextNode;
            }
        }
        listSize--;
    }
}

}

  • 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-15T07:42:30+00:00Added an answer on June 15, 2026 at 7:42 am

    Sure it can be done – you need to keep track of the number of objects you have passed in the way, and after you have passed n objects equals to the seeked one – insert the new data:

    public void addAfterNth(Object data,Object o, int n) { 
        Node curr = firstNode;
        while (curr != null) { 
            if (curr.data.equals(o)) n--;
            if (n == 0) { 
                Node newNode = new Node(data,curr.nextNode);
                curr.setNext(newNode);
                break;
            }
            curr = curr.getNextNode();
        }
    }
    

    In here you insert a new node with the data denoted in the parameter data after the nth encounter of a node with data equals to o.

    Running with:

    SingleLinkedList list = new SingleLinkedList();
    list.addLast(5);
    list.addLast(7);
    list.addLast(2);
    list.addLast(8);
    list.addLast(3);
    list.addLast(1);
    list.addLast(6);
    list.addLast(5);
    list.addLast(8);
    list.addLast(4);
    list.addLast(2);
    list.drawList();
    list.addAfterNth(999,8, 2);
    System.out.println("");
    list.drawList();
    

    yields (as expected):

    5, 7, 2, 8, 3, 1, 6, 5, 8, 4, 2, 
    5, 7, 2, 8, 3, 1, 6, 5, 8, 999, 4, 2, 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening, I am having a weird issue with a method I am trying
Good evening! I want to display a uncommon layout into a HTML Textfield, using
Good evening all, I'm trying to implement jquery table sorter, all seems fine and
Good evening, everyone. I've now run into an odd warning, and for the life
Good evening, I am still working on my project (trying to improve myself harder.)
Good evening guys. I'm currently trying to get started on development of a project
Good evening, I'm having an issue with Masonry. This is all my relevant code:
Good evening. I am doing a basic exercise to insert data into an Access
Good evening, I'm working on a project that has a list of seven options
Good evening, experts I want to solve recurrence equation using mathematica, x(n) = x(n

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.