0

Given the head of a linked list and the int to search for as parameters, I need a method that will remove the first occurrence of this number in the list, and return the modified list. however i cannot modify the original list. I know how to remove the node from the list, but im not sure how i would keep the original list intact since this has to be done recursively. below is the method ** initially M is the original list. I dont know that it will still be the same list after calling the method again...?

MyList removeNumber(MyList m, int removee){
3
  • Am I correct in assuming, that the parameter "MyList m" is the original list (i.e. the one you don't want to change) ? Commented Mar 31, 2016 at 0:23
  • Make a copy of the list as you recurse. Commented Mar 31, 2016 at 0:28
  • Yes m is the original list when the method is originally called. Im not sure that this will still be the case after calling the method recursively. Commented Mar 31, 2016 at 3:32

3 Answers 3

1

The idea is that the resulting structure will be a "Y": a two-headed list (actually a simple graph).

One branch of the Y is the original list. The other is your new list with removed node. The vertical stalk of the Y is what's after the element you remove. It's common to both lists. Here's some ascii art with the Y turned on its side showing a list of 1 to 5 with 3 removed.

     new -> 1 -> 2 ------\
                          v
original -> 1 -> 2 -> 3 -> 4 -> 5 -> null

Thinking recursively is all about defining a problem in terms of a smaller version of itself plus a fixed bit of work. And you need a base case (or maybe several).

A linked list is itself a recursive structure:

A list is either empty or it's an element linked by its "next" reference to a list.

Note this defines a list using a smaller list. The base case is the empty list. The fixed bit is the element.

Read this definition a few times, then see how it translates the code:

class MyList {
  int value;    // the element at the head of this list
  MyList next;  // the rest of the list

  MyList(int value, MyList next) {
    this.value = value;
    this.next = next;
  }
}

The base case "empty list" is just a null reference. The element removal problem expressed recursively using the same pattern becomes:

A copy of a list with an element removed is either a) the rest of the list following the head in the case that the element to be removed is the head or b) a copy of the current node followed by a copy the rest of the list with the desired element removed.

Here I'm defining a "copy of a list with one element removed" using a smaller version of the same thing. Case a) is the base case. The fixed bit is copying the head when it's not the removee.

Of course there's another base case: if the list is empty, the removee can't be found. That's an error.

Putting this in code:

MyList removeNumber(MyList m, int removee) {
  if (m == null) throw new RuntimeException("removee not found");
  if (m.value == removee) return m.next;
  return new MyList(m.value, removeNumber(m.next, removee));
}

Putting the function to use would look something like this:

MyList originalList = ... // list of 1 to 5.
MyList newListWith3removed = removeNumber(originalList, 3);

System.out.println("Original list:");
for (MyList p : originalList) System.out.println(p.value); 
System.out.println("With 3 removed:");
for (MyList p : newListWith3removed) System.out.println(p.value);

The output will look as expected: 1 to 5 in the first list and 1,2,4,5 in the second. I.e. the first list is unchanged.

Sign up to request clarification or add additional context in comments.

Comments

0
//This function will always return a new list with 'remove' removed
MyList removeNumber(MyList m, int remove){
//if m is empty List, return an empty list

//if head is not the int to remove, return a New list from 
//   head concat removeNumber(m.next,remove)
//else return removeNumber(m.next,remove)
}

Comments

0

I think it lacks information. I'm assuming however a very traditional implementation for linked list, for instance:

class MyList {

  MyList prev;
  MyList next;
  int data;

  static MyList removeNumber(MyList m,int removee) {

    if(m == null) return null; // already empty

    if(m.data == removee) { // m already is the node to throw away

      if(m.prev != null)// relink
        m.prev.next = m.next;

      if(m.next != null)// relink
        m.next.prev = m.prev;

      return m.prev;
    }
    // if this node isn't the one yet, keep looking for
    return removeNumber(m.next,removee); 
  }
}

There are plenty different ways to do that, but you have to provide more info in order to allow us to point you the correct literature.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.