Monday 18 May 2015

How To Remove Element of LinkedList

 We can remove specific element from linkedlist using remve(int index) method.

import java.util.LinkedList;

public class RemoveElement {
   
public static void main(String[] args) {
   
    LinkedList<String> linkedList = new LinkedList<String>();
   
    linkedList.add("Mumbai");
    linkedList.add("Delhi");
    linkedList.add("Noida");
    linkedList.add("Gao");
    linkedList.add("Patna");
    //print linkedlist
    System.out.println("Before Remove Element Linklist is = "+linkedList);
   
    /*
     * Removes the element at the specified position in this
       list. Shifts any subsequent elements to the left
     */
   
    linkedList.remove(2);
   
    System.out.println("After Remove Element Linklist is = "+linkedList);
}

}

Output:

Before Remove Element Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]

After Remove Element Linklist is = [Mumbai, Delhi, Gao, Patna]


No comments:

Post a Comment