Linked List

·

2 min read

Linked List is a linear data structure, in which elements are not stored at a contiguous location, rather they are linked using pointers. Linked List forms a series of connected nodes, where each node stores the data and the address of the next node.

A node is a user-defined data type (i.e. a class) that contains two things the first one is the data and the next one contains the address of the next node.

Head and Tail: The linked list is accessed through the head node, which points to the first node in the list. The last node in the list points to NULL indicating the end of the list. This node is known as the tail node.

Why linked list data structure needed?

Here are a few advantages of a linked list that is listed below, it will help you understand why it is necessary to know.

  • Dynamic Data structure: The size of memory can be allocated or de-allocated at run time based on the operation insertion or deletion.

  • Ease of Insertion/Deletion: The insertion and deletion of elements are simpler than arrays since no elements need to be shifted after insertion and deletion, Just the address needs to be updated.

  • Efficient Memory Utilization: As we know Linked List is a dynamic data structure the size increases or decreases as per the requirement so this avoids the wastage of memory.

  • Implementation: Various advanced data structures can be implemented using a linked list like a stack, queue, graph, hash map, etc.

Types of Linked Lists

Displaying the Linked list

where a is the head of the linked list. we should always use a function to print the linked list here a new node is created named 'temp' which is pointing towards a and as the loop iterates it points towards other nodes of the linked list, if we dont use functions and use a = a.next; then for the first time the linked list will get printed but we will lose the head of the linked list as a was the head which is changed now.

Using Recursion

DistplayLinked list in reverse order

Q. Implement a method to find out the length of the Linked List