cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Linked list

I am not clear about how linked lists work.

If I have several linked list intertwined:

(A)-[:ConnectsTo]->(M)-[:ConnectsTo]->(B)

(B)-[:ConnectsTo]->(M)-[:ConnectsTo]->(C)

It would appear as A is connected to C through M, but that is not what I intended.

Would it be possible to pull out two linked list as the way I code them?

A-M-B
B-M-C

where there is no A-M-C even though it appears so.

Thanks in advance

1 REPLY 1

You'd either use a different relationship type for each linked list or you don't build the list on the nodes directly and instead use proxy nodes:

 ()------->()------->()      
 |         |         |
(A)       (M)       (B)       (C)
           |         |         |
           ()------->()        ()
            ^                   |
            --------------------

I hope the diagram illustrates the idea:

  • your "domain" nodes are A,B,C,M
  • you don't build the list directly on those, instead for each entry in the linked list you create a separate node that connects to the domain node
  • you use CONNECTS_TO between the proxy nodes to have a linked list
  • the proxy nodes "belong" to the list. E.g. if you remove the list, also the proxy nodes get removed.
  • your lists are now (A)->(M)->(B) (top line) and (C)->(M)->(B) (bottom) as desired
  • following this each list is completely separated from any other list (since proxy nodes are not reused)