2

In this post, When is doubly linked list more efficient than singly linked list?, rici explains:

If deletion is not important, perhaps because the datastructures are immutable, singly-linked lists offer another really useful property: they allow structure-sharing. A singly-linked list can happily be the tail of multiple heads, something which is impossible for a doubly-linked list. For this reason, singly-linked lists have traditionally been the simple datastructure of choice for functional languages.

How can having multiple heads be a good thing?

Community
  • 1
  • 1

2 Answers2

3

It's not inherently a good thing. It's just the natural outcome of lazy copying of immutable linked-lists.

Imagine:

LinkedList a = createLinkedList(...);

LinkedList b = prepend(a, 3.14);

LinkedList c = prepend(a, 2.72);

If the contents are lazily copied (which is a natural choice if lists are immutable), then the first elements of b and c now both point at the first element of a.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

I'd say that if it makes sense to have multiple lists where parts of their tails are the same in your code, then representing them as lists that share those tails is a good thing, because it saves memory.

But, if something like that isn't useful for you, then this ability won't give you anything.

svick
  • 236,525
  • 50
  • 385
  • 514