hi

Part 3- Why we need pointers in Linked List | Linked List Tutorials in C#

 

In case of array, memory is allocated in contiguous manner, hence array elements get stored in consecutive memory locations. So when you have to access any array element, all we have to do is use the array index, for example arr[2] will directly access the 3rd memory location, returning the data stored there.

But in case of linked list, data elements are allocated memory at runtime, hence the memory location can be anywhere. Therefore to be able to access every node of the linked list, address of every node is stored in the previous node, hence forming a link between every node.


We need this additional pointer because without it, the data stored at random memory locations will be lost. We need to store somewhere all the memory locations where elements are getting stored.


Yes, this requires an additional memory space with each node, which means an additional space of O(n) for every n node linked list.






Share this

Related Posts

Previous
Next Post »