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

List prepend

Note that lists are great when we insert a node at the beginning, in other words, prepend a value to the head of a list. Let's see how that works.

We have the list with values 17, 199, and 337. We prepend the value 37 first. Next, we prepend 99 to the resulting list. Finally, we prepend 12 to the new resulting list again.

When we prepend the value 37, we just allocate the node; while constructing this node, we append the original list to 37. In other words, 37 becomes the head and the original list becomes the tail.

List prepend

Note that there is no copying needed at all. We just allocate the new node and append the existing list to it. As this ...

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