LinkedList

Singly LinkedList

What is it?

  • LinkedList is like a game of treasure-hunt where each point contains the clue for the next point

  • We need to start somewhere for the next point (node), therefore we are given with first node call head

  • A node contains 2 information (a node is a class object, from an implementation point of view):

    • value of the current node

    • pointing to the second node (address or class object)

# Here is how a single node for single LinkedList is defined, Node object (class)
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
  • The power of LinkedList comes from, ability to break the chain and rejoin it

  • Hence, unlike a list. we don't need to shift all the items after insertion, rather we can insert anywhere

  • Here is a disadvantage as well, index access is not efficient; we need to iterate from head till we reach the index to access it. Arbitrary access needs iteration from head to the point/index

  • Various Operations

Operation

Worst case

Average case

Search

O(n)

O(n)

Insert at the start

O(1)

O(1)

Deletion at the end

O(1)

O(1)

Implementation

Different Operations:

  • Insertion: at the start, in the end, in the middle

  • Traversing (accessing all the elements or search for a specific number)

  • Deletion: starting node

  • Delete key: find the key in LinkedList and delete the first found

  • Display: display the LinkedList

Doubly LinkedList

what is it?

  • In doubly LinkedList, we can move in either direction

  • A node has three parts in a doubly LinkedList

    • Data for the node

    • pointer to the next node

    • pointer to the previous node

  • We are given with start and end node in the case of LinkedList

  • We can travel in both forward or backward direction

  • Time complexity point of view: No difference if adding and deleting from start or end

Implementation

Circular LinkedList

What is it?

  • A circular LinkedList is a variation of a linked list in which the last element is linked to the first element. This forms a circular loop

  • It can be implemented using singly LinkedList or doubly LinkedList

    • for a singly LinkedList, the next pointer of the last item points to the first item

    • In the doubly LinkedList, prev pointer of the first item points to the last item as well

  • The NULL assignment is not required because a node always points to another node.

  • The starting point can be set to any node.

  • Traversal from the first node to the last node is quick

Implementation

Circular LinkedList using Singly LinkedList

Circular LinkedList using Doubly LinkedList

LinkedList Data Structure Uses

  • Dynamic memory allocation -

  • In undo functionality, it is more like a use case of queue using LinkedList

  • In Hash tables - When there is a collision of hash (two different values have the same hash then they are arranged as LinkedList within a hash)

  • In Tree -

  • In Graphs -

Miscellaneous

Difference Between the Array and LinkedList?

Reference for Further Reading

Last updated

Was this helpful?