3

I have an algorithm where I pass through nodes in a graph in a certain way, occasionally passing through the same node several times, and I need to form a list of the nodes passed, such that a node appears once for the last time I passed it.

For instance, if I passed through nodes A -> B -> C -> A -> C, the list I need in the end is [B, A, C].

What I wanted to do was to use a LinkedList, such that every node in the graph will contain a reference to its node in the LinkedList. Then, every time I pass through a node, I will remove its corresponding node from the LinkedList and insert it again into the end of the LinkedList, and the complexity of the operation will only be O(1).

However, when I began implementing this, I ran into a problem: apparently, the java class LinkedList does not allow me to see its actual list nodes. Using the regular remove functions of LinkedList to remove the list node containing a given graph node will be O(n) instead O(1), negating the whole point of using a LinkedList to begin with.

Naturally, I can implement LinkedList myself, but I would rather avoid that - it seems to me that if I have to implement LinkedList in java, I'm doing something wrong.

So, is there a way to solve this problem without implementing LinkedList on my own? Is there something that I'm missing?

5
  • 1
    You are right, the API sucks at that point. The internal working of the LinkedList is not public and even not constant in time. The Node class inside the LinkedList its name has already been changed name over time. I guess that your only option will be to create your own class. You could copy paste the LinkedList source code and modify it as you need. Commented Dec 25, 2013 at 12:59
  • The question is, how you are supposed to find the element from the list while re-inserting it ? Commented Dec 25, 2013 at 13:13
  • Each graph node holds a reference to its list node. When I pass through the graph node, I remove the node from the list using this reference, and then insert the graph node into the end of the list (and save the reference to the new list node, the one inserted into the end of the list). Commented Dec 25, 2013 at 13:21
  • @Sage if you write up your comments as an answer i would be able to give you credit for helping me... Commented Dec 26, 2013 at 23:20
  • @Nayish, sorry, actually i think you are right, i should post that as an answer for people who are likely to search in future. Thank you :) Commented Dec 27, 2013 at 6:25

3 Answers 3

1

As it seems, you are expecting a built-in approach, i don't think there is any Collection which provides such functionality. You will have to implement it on your own as @MartijinCourteaux suggested. Or:

  • use Sorted Set collection like TreeSet<E> with supporting cost of O(log n) for operations: add, remove and contains.
  • LinkedHashSet<E> But beware unlike HashSet<E>, LinkedHashSet can have O(1) expected performance for operations: add, contains, remove but the performance is likely to be just slightly below that of HashSet, due to the added expense of maintaining the linked list. But we can use it without incurring the increased cost associated with TreeSet. However, insertion order is not affected if an element is re-inserted into the set so try removing the first insertion of an element before re-inserting it.
Sign up to request clarification or add additional context in comments.

Comments

0

LinkedHashMap keeps order of entered values and allows remove node by its key and then put back to the end. I think that it is all you need.

Comments

0

Unless your linked list is large just using a regular array list will give fast performance even with the shuffling. You should also consider using hash sets, if order is not important, linked hash set if the order of insert matters, or tree set if you want it sorted. They don't allow duplicate values but have good O performance for insert, delete and contains.

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.