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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:18:27+00:00 2026-06-02T14:18:27+00:00

I am trying to remove an item from the LinkedList in java. This List

  • 0

I am trying to remove an item from the LinkedList in java. This List is implemented by me and I am not using any java API. The major trouble I am facing is with RECURSION as I am always lost in recursion coding.

class List{

    int N;
    List next;
    List current;
    List(int N){
        this.N =N;
        this.next = null;
    }

    @Override
    public String toString() {
        String o = "";
        List curr = this;
        while(curr != null){
            o += curr.N+"-->";
            curr = curr.next;
        }

        return o+"TAIL";
    }
}

Method implemented:

private static List Remove(List L,int N){
    if(L == null || L.next == null)
        return L;

    List current = L;
    List previous = null;

    while(current != null){
        if(current.N == N){
            current = current.next;
            if(previous == null)previous = current;
            else{
                previous.next = current;
            }
            break;
        }else{
            previous = current;
            current = current.next;             
        }   
    }
    return previous;
}

Input –

List list1 = new List(1);
        list1.next = new List(2);
        list1.next.next = new List(3);
        list1.next.next.next  = new List(4);
        list1.next.next.next.next  = new List(5);
        list1.next.next.next.next.next  = new List(6);
        list1.next.next.next.next.next.next  = new List(7);
System.out.println("Before Removal "+list1.toString());
        System.out.println("After Removal "+Remove(list1,3));

Output I am getting is –

  • Before Removal 1–>2–>3–>4–>5–>6–>7–>TAIL
  • After Removal 2–>4–>5–>6–>7–>TAIL

Here I am losing the value 1 as I am setting the current = current.next or reference is being set to next value. So definitely I am having some problem with the presentation of data stored in different references.

  • 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-02T14:18:29+00:00Added an answer on June 2, 2026 at 2:18 pm

    The mistake is here:

    return previous;
    

    You should return the original head of the list if it was not removed. To show it graphically:

    N == 3
    List Before Removal: 1-->2-->3-->4-->5-->6-->7-->TAIL
    At start of iteration 1:
    L                    ^
    previous      (null)
    current              ^
    No match -> iteration 2:
    L                    ^
    previous             ^
    current                  ^
    No match -> iteration 3:
    L                    ^
    previous                 ^
    current                      ^
    Match -> remove current:
    List After Removal:  1-->2-->4-->5-->6-->7-->TAIL
    L                    ^
    previous                 ^
    current                      ^
    

    At this point by returning previous, you lose the former head element L.

    For the case when the head element is to be removed, you should add a separate check before the loop.

    Btw your Remove method is not recursive – it is never calling itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

sampleList.RemoveAll(a=>a.reference.Contains(123)); This line of code does not remove any item from the list whereas
SORRY FOR THIS CONFUSION: UPDATED QUESTION: I'm trying to remove a list item from
I am trying to remove an item from listbox but is not working. even
I'm trying to remove an item from a sorted list. If the item is
In one of my projects I'm trying to remove an item from a list
I'm trying to remove an item from a list; but the item I'm trying
I have this code and am trying to remove an item from a nested
I'm trying to remove an item from an ArrayList and I get this Exception:
I am trying to remove an item from a listbox using C# code using
i'm trying to remove an item from a one to many list and have

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.