3

How to get the reference to the actual Node (entry) object from LinkedList - Not the value that it holds?
Something like this :

import java.util.LinkedList;

public class Main {

    public static void main(String[] args) {

        LinkedList<String> test = new LinkedList<String>();

        test.addLast("first");
        test.addLast("second");

        test.addLast("third");
        var thirdNodeReference = test.getLastNode(); // Does this even exist ?

        test.addLast("fourth");

        var secondNodeReference = thirdNodeReference.previous(); // To perform such operations.
    }

}

Does there exist a method like LinkedList.getLastNode() in java LinkedList so that one can perform previous() or next() operations on it?

I know LinkedList.listIterator() exists but that's not useful, because I'll be having references to each Node (entry), and I need to work with them - such as lastNodeReference in the code above.

If such a functionality doesn't exist in JAVA Standard Library, is there any 3rd party (external) Library that I can use?

Reason:

I need to access the Node to perform remove() operation in O(1).

In the actual JAVA code implementation it performs this in O(N) by traversing the list to find the Node containing the given object by performing equals() on every node on it's way. Also, check this comment.
This can be performed ideally in O(1) if we have a direct reference to Node object - because remove() only requires a change of 2 pointers of previous and next Node.

Dost Arora
  • 355
  • 3
  • 12
  • In what way will you be "working with them"? That is, what do you want to do that a `ListIterator` wouldn't permit you to do? – Kevin Anderson Sep 19 '20 at 12:54
  • there is no way you can access directly `Node` entries, it's not part of the public api of `LinkedList`, it's internal stuff. see here: http://hg.openjdk.java.net/jdk/jdk11/file/1ddf9a99e4ad/src/java.base/share/classes/java/util/LinkedList.java#l974, it is implemented as `private static class Node` – MarcoLucidi Sep 19 '20 at 12:57
  • @KevinAnderson So I need to maintain 2 `LinkedLists` (original, updated). And say I've got a reference to one of the MIDDLE nodes of `original` list : `someNode`. So, now I need to update it's String value -> pop it off the `original` list -> insert it in updated list at the end. It occurred to me, when I'm popping it off the original list, I'll have to update the `next` value of the previous node to point to next-of-next-of `someNode`. – Dost Arora Sep 19 '20 at 13:16
  • @MarcoLucidi Any 3rd party lib that has such functionality? – Dost Arora Sep 19 '20 at 13:24
  • How does that differ from just **removing** the node from `original`? Which you can already do with the `remove()` method on either a `ListIterator` or on the list itself. – Kevin Anderson Sep 19 '20 at 13:27
  • @KevinAnderson True, it can be done with the `remove()` method. But the problem is that is a `O(n)` operation, which ideally should not be if we have a reference to the actual Node - since it only involves a change of few pointers. See this [answer & comments](https://stackoverflow.com/a/42849573/8655856) and the [actual code](http://hg.openjdk.java.net/jdk/jdk11/file/1ddf9a99e4ad/src/java.base/share/classes/java/util/LinkedList.java#l359) implementation in JAVA library. It's an `O(n)` operation because it needs to traverse the list to find the node containing that object while performing `eq` – Dost Arora Sep 19 '20 at 13:34
  • Yes, it would be `O(n)` to remove a list item by its value. On top of `O(n)` to find the item in the first place, But with a `ListIterator` it's `O(n)` to find the item, then O(1)` to remove it, same as it would be if you were working directly with nodes. – Kevin Anderson Sep 19 '20 at 14:03
  • In fact, with a `ListIterator`, you _are_, effectively, working directly with the nodes; it just doesn't look that way. – Kevin Anderson Sep 19 '20 at 14:06
  • @DostArora, say you could get access to the nodes. What exactly would you do with that? Store them somewhere to make removal faster? Couldn't you just use that data structure in the first place? Also, are you sure these performance considerations are relevant? More often than not, data structures are so small that unless you're running `O(2^n)` it doesn't even matter. ;) – Nicolai Parlog Sep 22 '20 at 20:56

2 Answers2

1

There is a descendingIterator method on LinkedList, which is described as Returns an iterator over the elements in this deque in reverse sequential order, while it isn't (completely) clear what OP wants, Iterator does have a .next, .previous, and .remove methods.

hd1
  • 33,938
  • 5
  • 80
  • 91
0

a linked list can be represented as such:
enter image description here

so no, you can't get the previous element with a linked list. You might want to implement a double linked list tho ( exemples codes can be found quite easily on google)

safir
  • 84
  • 6
  • 1
    JAVA's LinkedList is implemented as [Doubly-Linked List](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html) – Dost Arora Sep 19 '20 at 12:47
  • Actually LinkedList in java implements Dequeue interface, so there must be a bi-linked list under the hood. But I've never thought about access to underlying nodes. – Azamat Zhurtbayev Sep 19 '20 at 12:50