An Intro to the Linked List. Time and again data structures and…
By: Sameer Nandan Menda
Tags: Data Structures, Linear Data Structure, Linked Lists, Programming

Linked List

A linked list is a collection of elements called nodes arranged in a linear fashion. Each node contains a data field and a pointer to the succeeding node in the list called next. The first and last node of a linked list usually are called the head and tail of the list. The head pointer points to the first node and the last node will point to a null value. Note that the head is not a separate node but a reference to the first node. When the list is empty, the head pointer points to null.

Structure of a Linked List

Linked lists are dynamic and flexible and can expand and contract in size. Memory for linked lists is allocated during execution or runtime.

Types Of Linked List

Singly Linked List: Being unidirectional in nature, the node traversal can be done in the forward direction only (see pic above). Doubly Linked List: Being bi-directional, the node traversal can be done in both forward and backward directions. Each node has an additional pointer called prev, pointing to the previous node. Circular Linked List: A variation of linked list in which the last element is linked to the first element forming a circular loop. It can take either of the above two forms. In a circular doubly linked list, the prev pointer of the head points to the tail and the next pointer of the tail points to the head.

Doubly Linked List

Circular Singly Linked List

Circular Doubly Linked List

Linked List Operations

Searching: To find the first element with the given value in the linked list by a simple linear search and return a pointer to this element. Insertion: To insert an element ‘x’ into the linked list is a multi-step process. An insertion can be done in 3 different ways: insert at the beginning of the list, insert at the end of the list and insert in the middle of the list. Deletion: To remove an element ‘x’ from the linked list is a multi-step process. A deletion can be done in 3 different ways: delete from the beginning of the list, delete from the end of the list and delete from the middle of the list. Traversal: To traverse all the nodes one after another. Updating: To update a given node. Sorting: To arrange nodes in a linked list in a specific order. Merging: To merge two linked lists into one.

Node Insertion

Shown below is an insert operation to the middle of a singly linked list:

Step 1: Create new node and Identify its location of insertion

Step 2: Set the pointer of the new node to the immediate next node

Step 3: Set the pointer of the previous node to the new node

New node inserted successfully in the middle of the list

Node Deletion

Shown below is a delete operation from the middle of a singly linked list:

Step 1: Identify the target node to be deleted

Step 2: Set the pointer of the target’s previous node to the target’s next node

Step 3: Remove the pointer of the target node

Updated list with target node removed | Pic courtesy: tutorialspoint.com

Time Complexity

A linked list is accessed via its head node. One has to only traverse through each node to reach the target node. Thus access is O(n).

Searching for a given value in a linked list similarly requires traversing all the elements until you find that value. Thus search is O(n).

Inserting into a linked list requires re-pointing the previous node to the inserted node, and pointing the newly-inserted node to the next node. Thus insertion is O(1).

Deleting from a linked list requires re-pointing the previous node (the node before the deleted node) to the next node (the node after the deleted node). Thus deletion is O(1).

Comparing performances of linear data structures , Pic Courtesy: bigocheatsheet.com

Advantages

Ease of insertion and deletion which takes O(1) time

Less memory wastage due to dynamic memory allocation

Disadvantages

Random access is not allowed

Requires extra memory space to store a reference to the next node

Applications