Linked List
Linked List
Like arrays, a Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a
contiguous location; the elements are linked using pointers. They include a series of connected nodes. Here,
each node stores the data and the address of the next node.
1
Search
Display
Representation of Singly Linked Lists:
A linked list is represented by a pointer to the first node of the linked list. The first node is called the head of
the linked list. If the linked list is empty, then the value of the head points to NULL.
Each node in a list consists of at least two parts:
A Data Item (we can store integers, strings, or any type of data).
Pointer (Or Reference) to the next node (connects one node to another) or An address of another node
In C, we can represent a node using structures. Below is an example of a linked list node with integer data.
In Java or C#, LinkedList can be represented as a class and a Node as a separate class. The LinkedList class
contains a reference of Node class type.
struct Node {
int data;
};
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Driver's code
int main()
{
2
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
// Function call
printList(head);
return 0;
}
Output
1 2 3
Time Complexity:
Search is O(n) because as data is not stored in contiguous memory locations so we have to traverse one by
one.
Insertion and Deletion are O(1) because we have to just link new nodes for Insertion with the previous and
next node and dislink exist nodes for deletion from the previous and next nodes without any traversal.
Auxiliary Space: O(N) [To store dynamic memory]