Why LinkedList is important?
What is LinkedList?
A linked list is a linear data structure similar to an array. In LinkedList, each element is a separate object that contains a link to the next object in that list.
Diagram of a LinkedList:
Advantages of LinkedList:
- Nodes can easily be removed or added from a linked list without reorganizing the entire data structure. This is one advantage it has over arrays.
Disadvantages of LinkedList:
- Search operations are slow in linked lists. Unlike arrays, random access to data elements is not allowed. Nodes are accessed sequentially starting from the first node.
- It uses more memory than arrays because of the storage of the pointers.
Code example of LinkedList:
class ListNode { constructor(data) { this.data = data this.next = null } } class LinkedList { constructor(head = null) { this.head = head } } let node1 = new ListNode(1) let node2 = new ListNode(2) node1.next = node2 let list = new LinkedList(node1) console.log(list.head.next.data)