Data Structure: Linked List

Data Structure: Linked List

What is a Linked List?

Simply a Linked List is a collection of data elements where every element points to the next element.

Using the technical terms a Linked List is a collection of Nodes where a node is a unit that contains data and a link to the next node. The first Node called the head, and the last one is the tail and it points to null.

image.png

benefit over Arrays and Hash-Table

  • Linked List are faster in operations like Insertion, access, delete than Arrays
  • Unlike Hash Table data in Linked List is ordered cause every element points to the next.

!That doesn't mean that this data structure is the best and you should use it all the time. Linked List has pros and cons too. So let's see the main cons.

Cons of Linked List

  • Having pointer alongside data means using more memory.
  • To read one specific node u need first read all nodes starting from the head.
  • computer use cashing to read from sequential memory which makes it really fast but in the case of Linked List our Nodes are stored noncontiguously so we can't benefit from that cashing system.

And there is some more but u get the idea. It's not perfect, it's indeed useful and straightforward but you need to think about the pros and cons and if u should use it.

Performance

  • prepend : 0(1)
  • append : 0(1)
  • lookup: 0(n)
  • insert: O(n)
  • delete: O(n)

Doubly Linked List

The only difference between doubly and singly Linked List is that Doubly has 2 different pointers in each Node, one like in singly would point to the next node and the other one point to the previous one. with that being said Doubly Linked List requires more memory than Singly LL but it's more efficient cause it can be accessed in both directions.

image.png

This is all for Linked List. I hope I explain fine and simplify it enough for you. :)