In this post I want to share the Linked List implementation in Java. Primarily operations such as insert,delete,finding length,printing length of linked list is covered via different set of operations.
Insert and Delete method covers all 3 scenario which are
1. Insert/Delete at beginning
2. Insert/Delete at end
3. Insert/Delete at middle
Here we use 2 classes to do this. Below is the ListNode.java file
package abhishek.linkedlist;
public class ListNode {
private int data;
private ListNode next;
public ListNode(int data){
this.data=data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public ListNode getNext() {
return next;
}
public void setNext(ListNode next) {
this.next = next;
}
}
Following class LinkedListOperation will use ListNode to implement various operations supported by linked list
package abhishek.linkedlist;
public class LinkedListOperation {
static ListNode headnode;
public static void main(String[] args) {
insert();
System.out.println("Linked List After Insertion");
printlist();
delete();
System.out.println("Linked List After Delete");
printlist();
}
public static void insert(){
ListNode node = new ListNode(10);
headnode =inserttoll(headnode,node,1);
ListNode node1 = new ListNode(20);
headnode =inserttoll(headnode,node1,1);
ListNode node2 = new ListNode(30);
headnode =inserttoll(headnode,node2,1);
ListNode node3 = new ListNode(100);
headnode =inserttoll(headnode,node3,4);
ListNode node4 = new ListNode(50);
headnode =inserttoll(headnode,node4,4);
}
public static void delete(){
headnode = deletefrmll(headnode,1);
headnode = deletefrmll(headnode,2);
}
public static ListNode inserttoll(ListNode headnode,ListNode node,int p){
if(headnode==null)
return node;
int size = listlen(headnode);
if(p>size+1 || p<1){
System.out.println("Invalid Position Valid is 1 to "+(size+1));
return headnode;
}
if(p==1){
node.setNext(headnode);
return node;
}else {
ListNode prevNode = headnode;
int count =1;
while(count<p-1){
prevNode = prevNode.getNext();
count++;
}
ListNode currNode = prevNode.getNext();
node.setNext(currNode);
prevNode.setNext(node);
}
return headnode;
}
public static ListNode deletefrmll(ListNode headnode,int p){
int size = listlen(headnode);
if(p>size+1 || p<1){
System.out.println("Invalid Position Valid is 1 to "+(size+1));
return headnode;
}
if(p==1){
ListNode node = headnode.getNext();
headnode =null;
return node;
}else {
ListNode prevNode = headnode;
int count =1;
while(count<p-1){
prevNode = prevNode.getNext();
count++;
}
ListNode currNode = prevNode.getNext();
prevNode.setNext(currNode.getNext());
currNode = null;
}
return headnode;
}
public static int listlen(ListNode headnode){
int len = 0;
while(headnode!=null){
len++;
headnode = headnode.getNext();
}
return len;
}
public static void printlist(){
String str = "{";
ListNode lheadnode = headnode;
while(lheadnode!=null){
str = str + lheadnode.getData() + " ";
lheadnode = lheadnode.getNext();
}
System.out.println(str.trim()+ "}");
}
}
Output for the above code is
Linked List After Insertion
{30 20 10 50 100}
Linked List After Deletion
{20 50 100}