Linked List Insertion and Deletion in Java

Singly Linked List Insertion and Deletion in Java

In this post, we will look at all possible ways of insertion and deletion of a node in a Single Linked List in Java. Both insertions and deletion in a Linked List can happen at the following positions-

  • At Front
  • At End
  • In the Middle (After a certain position)

What is a Linked List

A linked list is a chained sequence of data structures holding some data value and connected to one another sequentially. 

Each node contains the following –

  • Data value
  • Next – Holds address to the next node
Linked List insertion and Deletion in Java

Possible positions to insert/delete in a Linked List

Both insertions and deletion in a Linked List can happen at the following positions-

  • At Front
  • At End
  • In the Middle (After a certain position)

We will look at the program with all three above functions, however, do note that the default nature of a Linked List is to always insert at the front.q

Structure of a Linked List in Java

// Node Class
class Node{
    int data;
    Node next;

    Node(int x) // parameterized constructor
    {
        data = x;
        next = null;
    }
}

Insertion in Singly Linked List in Java

Let us have a look at the programs below –

Output

1 2 3 
1 2 3 5 6 7 8
1 2 3 25 5 6 7 8

Deletion in a Linked List in Java

Let us have a look at the programs below –

Output

1 2 3 4 5 6

Deleted: 1
2 3 4 5 6

Deleted: 6
2 3 4 5

Deleted: 4
2 3 5

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription