CodesJava

Easy learning with example program codes

Reverse linked list in java

We can use Collections.reverse() method to reverse linked list in java.

Syntax:

Collections.reverse(linkedList);

Example:

package com.codesjava;
 
import java.util.Collections;
import java.util.LinkedList;
 
public class Test {
  public static void main(String args[]){
	LinkedList<String> linkedList = new LinkedList<String>();
	linkedList.add("Jai");
	linkedList.add("Mahesh");
	linkedList.add("Naren");
	linkedList.add("Vivek");
	linkedList.add("Vishal");
	linkedList.add("Hemant");
	System.out.println("Actual LinkedList:"+linkedList);
	Collections.reverse(linkedList);
        System.out.println("Results after reverse operation:" + linkedList);
  }
}

Output

Actual LinkedList:[Jai, Mahesh, Naren, Vivek, Vishal, Hemant]
Results after reverse operation:[Hemant, Vishal, Vivek, Naren, Mahesh, Jai]
Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status