You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Linked List is a linear data structure where each node is a separate object.
Each node has three items - the data and a reference to the previous and next node.
In Linked List the data is not stored in contagious memory like in arrays.
The number of nodes in a list is not fixed and can grow and shrink on demand.
In the Doubly Linked List, we maintain two pointers; one for next node and another for previous node. This way we will be able to iterate backward or forward any time we want.
Pros & Cons
Pros
Memory Management: LinkedLists dose not require contiguous blocks of memory and therefore can help reduce memory fragmentation.
Useful if you need a lot of insertions at the beginning of the list. It's faster than array.
Array: need to shift all the elements by 1 element
Linked List: we don't keep track of the postions of the elements
Cons
Linked List does not allow direct access to the individual nodes. If you want to access a particular node then you have to start at the head and follow the references until you get to that node.
Another disadvantage is that a Linked List uses more memory compare with to an array because each node store extra information about the reference to the prev or next node.
Big O Comparisons (Average Case)
Point
Doubly Linked List
Array
Element Acesss
O(n) - sequential access
O(1) - direct access; O(n) - squential access
Insertion / Deletion
O(1)
O(n) - because of shifting items
Search Elements
O(n)
O(n) - unsorted; O(log n) sorted
Insertion at middle is O(1), but getting to that middle node is O(n).
Applications in real world
Image Viewer: Previous and next images are linked, hence can be accessed by next and previous button.
Previous and Next Page in Web Browser: We can access previous and next url searched in web browser by pressing back and next button since, they are linked.
Music Player: Songs in music player are linked to previous and next song. You can play songs either from starting or ending of the list.