share

Sunday, 17 May 2015

How to reverse elements of LikedList


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

public class ReverseAllelement {
   
    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 Reverse Linklist is = "+linkedList);
       
        //Reverses the order of the elements in the specified list.

        Collections.reverse(linkedList);
       
        System.out.println("After Reverse Linklist is = "+linkedList);

    }
   

}

Output:

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

After Reverse Linklist is = [Patna, Gao, Noida, Delhi, Mumbai]


No comments:

Post a Comment