Tag Archives: Reverse

Collections.reverse() in Java with Examples

java.util.Collections.reverse() method is a java.util.Collections class method. It reverses the order of elements in a list passed as an argument. // Reverses elements of myList and returns Nothing. // For example, if list contains {1, 2, 3, 4}, it converts // list to {4, 3, 2, 1} public static void reverse(List myList) It throws UnsupportedOperationException… Read More »

Reverse and Add Function

The reverse and add function starts with a number, reverses its digits, and adds the reverse to the original. If the sum is not a palindrome, repeat this procedure until it does. Write a program that takes number and gives the resulting palindrome (if one exists). If it took more than 1,000 iterations (additions) or… Read More »

Reverse an array in groups of given size | Set 2 (Variations of Set 1 )

Given an array, reverse every sub-array that satisfies the given constraints. We have discussed a solution where we reverse every sub-array formed by consecutive k elements in Set 1. In this set, we will discuss various interesting variations of this problem.   Variation 1 (Reverse Alternate Groups): Reverse every alternate sub-array formed by consecutive k… Read More »

How to quickly reverse a string in C++?

One simple solution is two write our own reverse function to reverse a string in C++. Output : skeegrofskeeg There is a direct function in “algorithm” header file for doing reverse that saves our time when programming. // Reverses elements in [begin, end] void reverse (BidirectionalIterator begin, BidirectionalIterator end); Below is simple C++ program. Output… Read More »

Merge two sorted linked lists such that merged list is in reverse order

Given two linked lists sorted in increasing order. Merge them such a way that the result list is in decreasing order (reverse order). Examples: Input: a: 5->10->15->40 b: 2->3->20 Output: res: 40->20->15->10->5->3->2 Input: a: NULL b: 2->3->20 Output: res: 20->3->2 We strongly recommend that you click here and practice it, before moving on to the… Read More »

Reverse an array without affecting special characters

Given a string, that contains special character together with alphabets (‘a’ to ‘z’ and ‘A’ to ‘Z’), reverse the string in a way that special characters are not affected. Examples: Input: str = “a,b$c” Output: str = “c,b$a” Note that $ and , are not moved anywhere. Only subsequence “abc” is reversed Input: str =… Read More »