Skip to main content

Deleting a Node by passing data in LInkedList

Deleting a Node by passing data in LInkedList:-

The node can be deleted by passing the data value in a function.Here we have three pointers one to traverse the list other to point current node and third one to point previous of current node.



//deleting a given key
 public Node deleteKeyNode(Node head,int key)
 {
  //traverse pointer
  Node temp=head;
  //previous pointer
  Node prevtemp=temp;
  
  while(temp!=null)
  {
   //when key is at head position
   if(key==head.data)
   {
    head=head.next;
    System.out.println("head is deleted");
    return head;
   }
   
   //when key is at any position other than head
   if(temp.data==key)
   {
    prevtemp.next=temp.next;
    temp.next=null;
    temp=prevtemp; 
   }
   prevtemp=temp;
   temp=temp.next;
  }
  return head;
 }

Comments

.

Popular posts from this blog

BREADTH FIRST SEARCH IN JAVA

BREADTH FIRST SEARCH IN JAVA:- package com . problems . graph ; import java.awt.DisplayMode ; import java.util.Iterator ; import java.util.LinkedList ; public class BFSGraph { int maxsize ; Vertex vertexlist []; int matrixlist [][]; int vertexcount ; @SuppressWarnings ({ "rawtypes" , "unused" }) LinkedList queue ; public BFSGraph () { maxsize = 20 ; matrixlist = new int [ maxsize ][ maxsize ]; vertexlist = new Vertex [ maxsize ]; for ( int i = 0 ; i < maxsize ; i ++) { for ( int j = 0 ; j < maxsize ; j ++) { matrixlist [ i ][ j ]= 0 ; } } queue = new LinkedList (); } public void addVertex ( char label ) { vertexlist [ vertexcount ++]= new Vertex ( label ); } public void addEdge ( int i , int j ) { matrixlist [ i ][ j ]= 1 ; matrixlist [ j ][ i ]= 1 ; } public void displayVertex ( int v ) { System . out . println ( vertexlis

How to do Effective Programming?

There is no secret to doing effective programming but following a sequence of steps and procedures can help in building great programs. Today I will take you on a tour to some of the best practices that are integrants of perfect programs. Algorithms are important: - You can’t deny the fact that algorithms are important and before start to write any program, Algorithms are must to write. For example, if there is a program about finding the sum of two numbers? What will you write ?The steps can be :- 1)   Get the first number. 2)   Get the second number. 3)   Add both numbers. This is what algorithm is writing about ,a list of statements .From the above three statements you can conclude boundary cases (at least two number should be there in input), mathematical function(Sum is needed here) , storage capacity(amount of memory to be assign to the variables), number of methods by analyzing repeat steps (reduce replete codes) and many other things. During algorithm only yo