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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:51:45+00:00 2026-06-14T15:51:45+00:00

So i have a linked list that I want to be able to remove

  • 0

So i have a linked list that I want to be able to remove the first occurrence of a number,

I’m trying to use recursion but sadly all I end up doing is being able to delete the head of the list and

public List remove(int num){
   if(value == num) {
       return next.remove(value);
   }else{
       next = next.remove(value);
       return this;
   }
}

I know i need to return the new list, but how exactly do i either get rid of the node that I’m trying to avoid or is there a way to work around it, so it continues to the next nod.

Edit. Update on the actual code.

class List{
  int value;  //value at this node 
  List next;  //reference to next object in list
  public List(int value, List next){
      this.value = value;
      this.next  = next;
  }
}

I have three different classes, one for the empty list at the end of this, and a class declaring this method, and the actual list.

  public static List makeSample() {
        EmptyList e = new EmptyList();
        List l1 = new List(5, e);
        List l2 = new List(4, l1);
        List l3 = new List(3, l2);
        List l4 = new List(3, l3);
        List l5 = new List(2, l4);
        List l6 = new List(1, l5);
        return l6;
    }
  • 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-14T15:51:47+00:00Added an answer on June 14, 2026 at 3:51 pm

    Try this

    import static org.junit.Assert.assertEquals;
    
    import org.junit.Test;
    
    public class List {
    
        private int value;
        private List next;
    
        public static final List EMPTY = new List(-1, null) {
            public List remove(int n) { return this; };
            public String toString() { return ""; };
        };
    
        public List(int value, List next) {
            this.value = value;
            this.next = next;
        }
    
        public List remove(int n) {
            if (value == n) return next;
            return new List(value,next.remove(n));
        }   
    
        public String toString() {
            return value + "," + next.toString();
        }
    
        public static class Examples {
    
            @Test
            public void shouldRemoveElement() {
                List l = new List(1, new List(2, new List(2, new List(3, EMPTY))));
                assertEquals("1,2,2,3,",l.toString());
                assertEquals("2,2,3,",l.remove(1).toString());
                assertEquals("1,2,3,",l.remove(2).toString());
                assertEquals("1,2,2,",l.remove(3).toString());
                assertEquals("1,2,2,3,",l.toString());
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic linked-list that holds data of type void* I am trying
I want to write a linked list that can have the data field store
I have a list that I want to be able to store 20 values.
I have a Linked List and I want to implement a function: Random_Shuffle_List (struct
I have an object that contains, as part of its data, a linked list.
I'm making a linked list (of structs) in C, but I want to be
I have a linear linked list that consists of nodes: class Node{ Object data;
I have a class that implements a linked list. The class has a find()
i want to implement this in c#, i have my linked list class ready,
Say I want to work with a linked list in java. I thought that

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.