O'Reilly logo

Learning Functional Data Structures and Algorithms by Raju Kumar Mishra, Atul Khot

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Concatenating lists

In the imperative world, where we perform mutation as needed, concatenating two linked lists is easy.

Given the two lists a and b, we just traverse the first list a until we reach its last node. Then we change its next pointer to the head of list b:

Concatenating lists

Note what happened to the original list a. It changed. The original list simply does not exist anymore.

We destroyed list a when we connected its third node to list b. The preceding list mutation is also not thread-safe. As seen in the previous chapter, additional mechanism, such as locking, is needed to make sure the state is synchronized correctly.

We could do the concatenation by ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required