import java.util.LinkedList;
public class LinkedListReversePractice {
public static void main(String[] args) {
LinkedList<Integer> numbers = new LinkedList<Integer>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
System.out.println("LinkedList before reversion:- "+numbers);
numbers = reverseLinkedList(numbers);
System.out.println("LinkedList after reversion:- "+numbers);
}
private static LinkedList<Integer> reverseLinkedList(LinkedList<Integer> numbers) {
for(int i = 0; i< numbers.size()/2; i++) {
int temp = numbers.get(i);
numbers.set(i, numbers.get(numbers.size()-1-i));
numbers.set(numbers.size()-1-i, temp);
}
return numbers;
}
}
Output:-
LinkedList before reversion:- [1, 2, 3, 4, 5]
LinkedList after reversion:- [5, 4, 3, 2, 1]
Thursday, August 5, 2021
How to reverse linked list without using inbuild function or extra loop
Subscribe to:
Post Comments (Atom)
How to find middle node of LinkedList without using extra memory in java
To find the middle node of a LinkedList without using extra memory in Java, you can use the "tortoise and hare" algorithm. This al...
-
public class ReverseArrayWithoutExtraMemory { public static void main(String[] args) { int arr[] = {1,2,3,4,5,6}; int swapArray[] =...
-
1) List Interface Implementation public interface List<T extends Comparable<T>> { public Node<T> getMi...
-
function data_change(field) { var check = true; var value = field.value; //get characters //check that all ch...
No comments:
Post a Comment