Most common definition of Linked lists is that they are linear or sequential data structures in which elements are stored at non-contiguous memory locations and are linked to one another using pointers.
A Linked List is a sequence of links that contains items. Each link contains a connection to another link. Linked lists are the second most used data structure after arrays.
Like arrays, linked lists are also linear data structures, but in linked lists, elements are not stored at contiguous memory locations. They can be stored anywhere in the memory, but for sequential access, the nodes are linked to each other using pointers.
Below are few terms that are useful for understanding more about LinkedList:
- Link: Each link of a linked list contains data of a node called an element.
- Next: Each link contains the address(link) of the next element called next.
Types of Linked List
Following the types of linked list:
- Single Linked List: Nodes can be navigated in forward direction.
- Doubly Linked List: Nodes can be navigated in forward as well as backward direction.
- Circular Linked List: Last node has the address(link) of First node as next and First node has the address(link) of the last node as prev.
Singly Linked List
Basic Operations
- Insertion: add an element to the list.
- Deletion: delete an element from the list.
- Display: display list.
- Search: search an element from the list.
Basic code of single linked list:
class LinkedList {
Node head;
static class Node {
int data;
Node next;
//constructor
Node(int newData) {
data = newData;
next = null;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList(); //empty Linked List
//inserting 3 nodes with data
list.head = Node(10);
Node secondNode = Node(20);
Node thirdNode = Node(30);
//providing address(link) to the nodes
list.head.next = secondNode; //Linked first node to the second node
secondNode.next = thirdNode; //Linked second node to the third node
}
}
Insertion Operation
As we already have a head node of a linked list, our goal is to insert new nodes in the already created linked lists.
There can be many conditions/situations you may run into while inserting a node. The frequent ones are listed below:
- Insert a node at the start of the list.
- Insert a node after any other node.
- Insert a node at the end of the list.
Insert a node at the start of the list
Inserting a node at the start of the list can be done by inserting the node before the head of the list, and then the newly added node will become the head of the given list.
The below function is in the above sample code of Linked List.
public void insertAtFirst(int data) {
//Allocate and insert data
Node atFirst = new Node(data);
//Point next of the newly added node to head
atFirst.next = head;
//Point head to the newly added node
head = atFirst;
}
Time Complexity of inserting the node at first is O(1).
Insert a node after another node
Inserting a node after another node is similar to inserting a node first. We have to create a new node and assign the pointer of the newly inserted node with the previous node and the next pointer of the previous node to the newly created node.
The below function is in the above sample code of Linked List.
public void insertAfterNode(Node previousNode, int data) {
//check if previous Node is null
if (previousNode == null) return;
//Allocate and insert data
Node newNode = new Node(data);
//Point next of the newly added node to head
newNode.next = previousNode.next;
//Point next to the newly added node
previousNode.next = newNode;
}
Time Complexity of inserting the node after another node is O(1).
Insert a node at the end of the list.
Inserting a node at the end of the list is done by always adding the new node at the last node of the list. For this, we must traverse the linked list to the end in order to get the pointer pointing to the last node.
The below function is in the above sample code of Linked List.
public void insertAtLast(int data) {
//Allocate and insert data
Node newNode = new Node(data);
//check if head is null then assign new node to head
if (head == null) {
head = newNode;
return;
}
//Assign next of new node to null(as last node)
newNode.next = null;
Node lastNode = head;
//traverse till the last node to get pointer of last node
while (lastNode.next != null) {
last = last.next;
}
//change next of the last node to the n
last.next = newNode;
return;
}
Time Complexity of inserting the node at the end is O(n).

Join the conversation! Your thoughts help the community grow.