0

I need to add a node at the front of the linked list using recursion.

Below is the add method I'm trying to implement. I could only figure out how to add it at the back of the linked list :(

    public void add(E element)
    {
        Node<E> newNode = new Node<E>(element, null);

        if (this.next == null)
        {
            this.next = newNode;
        } else {
            next.add(element);
        }
    }
4
  • 1
    what's the use of recursion here? I don't understand. Commented Oct 6, 2013 at 16:04
  • 3
    Draw what you need to do with the new node and the existing ones on a piece of paper. That'll make things clearer. Also, we can't read what is on your screen, and thus can only guess what the Node class looks like. Commented Oct 6, 2013 at 16:06
  • 1
    The answer is going to vary quite a bit depending on issues such as: Is there a header Node? Or is there a list class other than Node? Are the lists mutable or immutable? Can add return something or must it return void as above? Commented Oct 6, 2013 at 16:14
  • There's no need for recursion IMO, but we don't understand your implementation entirely. Simply, you're taking the your new reference, pointing it to the head, then having your new reference become the new head reference. Nothing recursive in nature about that at all... Commented Oct 6, 2013 at 16:16

4 Answers 4

1

To add an item to the front of a single-linked list you create a new node and make it point to the first node of the list.

This new node is now the new first node of the linked list.

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

2 Comments

would this be adding it at the beginning of the list? Node<E> newNode = new Node<E>(element, next);
@lauraxx that depends on the value of next
1

It should be pretty simple, much easier than adding of the element to the end.

Something like this should work:

public void addToFront(E element) {
    element.next = rootElement;
    rootElement = element;
}

Comments

0

The algorithm should be something like this

 public void add(E element)
    {
        Node<E> newNode = new Node<E>(element, null);
      if (root == null) 
      { 
         root = new Node(element, null);
      }
      else 
      {
         Node temp = new Node(element, null);
         temp.next = root;
         root = temp;
      }
    }

Comments

0
public Node<E> addFirst(E element)
{
    return new Node<E>(element, this.next);
}

Node<String> list;
...
list = list.addFirst("a"):

In the java SE another trick was done: make an outer container class LinkedList with a field "Node". This enables maintaining a counter for size(). This is a stateful class.

The above could be used for an immutable list class, which is good for concurrency.

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.