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]

 

Leave a Reply

Your email address will not be published. Required fields are marked *

74 − = 64