Question: ```
class Link{
    private int value;
    private Link next;
}

```

I am asked to write a recursive method to delete last occurrence of a certain value, say 4.

before 2->3->4->5->4->2
after 2->3->4->5->2

The last occurrence only. I know how to delete all occurrence but I can't tell if its the last occurrence. No helper method is allowed.

The one to delete all occurrence

```
public Link deleteAll(){
    if (next == null){
        return value==4？ null:this;
    }else{
        if (value == 4){
            return next.deleteAll();
        }
        next = next.deleteAll();
        return this;
    }
}

```

Answer: 
You can declare a pointer to the last occurred node and delete that node when reached the last element in list. Following steps explains that - 

1. Declare two pointers one is next as in your above code another can be temp.
2. Iterate through list using next like you doing in deleteAll method above.
3. If you find the node you looking for assign that node to temp.In your case 4.
4. When next is null you reached the end of list now delete, whatever node is in temp delete that node. If temp is still null than no node found in given key.

EDIT:
 Possible pseudo Code in case of recursion:

```
    public void deleteLast(Node node,Node temp,Node prev, int data)
    {
        if(node==null)
        {
          if(temp!=null && temp.next.next!=null){
          temp.next = temp.next.next;}
          if(temp.next.next==null)
          temp.next = null;
          return;
        }
        if(node.data==data)
        {
          temp = prev;
        }

        prev = node;
        deleteLast(node.next, temp, prev, int data);

    }

```

Above code should be able to solve your problem. I made some edit in my approach which should be obvious from the code but let me describe it below

* I added a prev pointer. Because if we want to delete a particular node we need to assign its next to prev node's next.So, we need the prev node not the node that we want to delete.

I think this change will follow in iterative approach too.
Not really answering your exact question, but as an alternative option, you might consider the following.

Write a recursive method to delete the first occurrence of a specified value, something like this:

```
public Link deleteFirst(int target) {
  if (value == target) {
    return next;
  }
  next = (next == null) ? null : next.deleteFirst(target);
  return this;
}

```

Then you could write a `reverse()` method as either an iterative or recursive method as you see fit. I haven't included this, but googling should show some useful ideas.

Finally the method to remove the last occurrence of a value from the linked list could then be written like this:

```
public Link deleteLast(int target) {
  return reverse().deleteFirst(target).reverse();
}

```

Note that as long as your `reverse()` method is linear complexity, this operation will be linear complexity as well, although constants will be higher than necessary.