Showing posts with label LinkedList. Show all posts
Showing posts with label LinkedList. Show all posts

How to delete a Node from Linked List in java

In our earlier tutorials we have seen lot of Linked List related questions and programs. On similar stage another question is how to delete a Node from a Linked List. Same way lets create a Linked List and try to delete a node from it with simple Java code.

How to delete a Node from Linked List in java


class NODE {

 int data;
 NODE next = null;

 public NODE(int data) {
  this.data = data;
 }
}

public class DeleteLLNode {
 
 public static void main(String[] args) {

  int array[] = new int[] { 11, 12, 13, 14, 15, 16, 17, 18 };
  int deleteNode = 15;
  
  DeleteLLNode obj = new DeleteLLNode();

  // Create linkedlist based all array values
  NODE start = obj.createLinkedList(array);
  
  // Print all node values before delete
  obj.printLinkedList(start);
  
  start = obj.deleteNode(start, deleteNode);

  // Print all node values after delete
  obj.printLinkedList(start);
 }

 /*
  * Create LinkedList and return the start pointer/node
  */
 public NODE createLinkedList(int[] array) {

  NODE start = null;
  NODE tmp = null;

  for (int i : array) {

   tmp = new NODE(i);

   if (start == null) {
    start = tmp;
   } else {
    NODE mover = start;
    while (mover.next != null) {
     mover = mover.next;
    }
    mover.next = tmp;
   }
  }
  return start;
 }
 
 /*
  *  Print all linked list node values 
  */
 public void printLinkedList(NODE start) {
  System.out.print("Linked List: ");
  while(start != null) {
   System.out.print(start.data +", ");
   start = start.next;
  }
  System.out.println();
 }

 /*
  * Delete Linked List node 
  */
 public NODE deleteNode(NODE start, int val) {
  NODE tmp = start;
  
  // If its first node to delete
  if(tmp != null && tmp.data == val) {
   start = start.next;
   return start;
  }
  
  NODE previous = null;
  while(tmp != null) {
   if(tmp.data == val) {
    previous.next = tmp.next;
   }
   previous = tmp;
   tmp = tmp.next;
  }
  return start;
 }
}


OUTPUT:


Linked List: 11, 12, 13, 14, 15, 16, 17, 18, 
Linked List: 11, 12, 13, 14, 16, 17, 18, 


How to create simple and easy singly LinkedList in Java

Lets see simple Java code to create custom singly LinkedList and maintaining the same order. Also lets traverse the LinkedList and make sure the LinkedList created properly or not. Here we have class called NODE to store the node details like data and next link. MyLinkedList class used to create LinkedList and to print all the values in LinkedList by traversing all nodes from start to end.

How to create simple and easy singly LinkedList in Java



class NODE {

 int data;
 NODE next = null;

 public NODE(int data) {
  this.data = data;
 }
}

public class MyLinkedList {

 public static void main(String[] args) {

  int array[] = new int[] { 11, 12, 13, 14, 15, 16, 17, 18 };

  MyLinkedList obj = new MyLinkedList();
  
  //Create linkedlist based all array values
  NODE start = obj.createLinkedList(array);
  
  //print all the values in linkedlist
  obj.traverseLinkedList(start);
 }

 /*
  * Create LinkedList and return the start pointer/node
  */
 public NODE createLinkedList(int[] array) {

  NODE start = null;

  for (int i : array) {

   NODE tmp = new NODE(i);

   if (start == null) {
    start = tmp;
   } else {
    NODE mover = start;
    while (mover.next != null) {
     mover = mover.next;
    }
    mover.next = tmp;
   }
  }
  return start;
 }

 /*
  * Print/traverse the linkedlist all values 
  */
 public void traverseLinkedList(NODE start) {
  while (start != null) {
   System.out.println(start.data);
   start = start.next;
  }
 }
}


OUTPUT:


11
12
13
14
15
16
17
18

Reverse LinkedList

Reverse Linkedlist using java code

This is one of the interview question which asked in recent interview like need to create a LinkedList and need to reverse those values in same linkedList without using addition list. We can make use of List implementation class LinkedList to achieve this. There are couple of ways we can achieve this in java by using Collection utility function reverse() and without reverse() function also. 
Lets see both the implementation in below examples. First lets see how to reverse LinkedList by using reverse() function. 


import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class LinkedListReverse {

 public static void main(String[] args) {
  
  List<Integer> list = new LinkedList<Integer>();
  list.add(3);
  list.add(6);
  list.add(7);
  list.add(0);
  list.add(1);
  list.add(4);
  list.add(9);
  list.add(5);
  
  System.out.println("Before Reverse : "+list.toString());
  
  Collections.reverse(list);
  
  System.out.println("After Reverse : "+list.toString());  
 }
}

OUTPUT:


Before Reverse : [3, 6, 7, 0, 1, 4, 9, 5]
After Reverse : [5, 9, 4, 1, 0, 7, 6, 3]

Next we will see how to reverse linkedlist without using reverse() function.


import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class LinkedListReverse {

 public static void main(String[] args) {
  
  List<Integer> list = new LinkedList<Integer>();
  list.add(3);
  list.add(6);
  list.add(7);
  list.add(0);
  list.add(1);
  list.add(4);
  list.add(9);
  list.add(5);
  
  System.out.println("Before Reverse : "+list.toString());
  
  ListIterator<Integer> itrFirst = list.listIterator();
  ListIterator<Integer> itrLast = list.listIterator();
  
  while (itrLast.hasNext()) {
   itrLast.next();   
  }
  
  for(int i = (list.size()/2)-1;itrFirst.hasNext() && itrLast.hasPrevious() && i>=0;i--){
   int first = itrFirst.next();
   int last = itrLast.previous();
   itrFirst.set(last);
   itrLast.set(first);
  }
  
  System.out.println("After Reverse : "+list.toString());  
 }
}

OUTPUT:


Before Reverse : [3, 6, 7, 0, 1, 4, 9, 5]
After Reverse : [5, 9, 4, 1, 0, 7, 6, 3]