Showing posts with label reverse. Show all posts
Showing posts with label reverse. Show all posts

Tuesday, May 1, 2018

Reverse a LinkedList

Problem statement: Reverse a LinkedList in java !
  1. class Node5 {
  2. Node5 next;
  3. int data;

  4. Node5(int data) {
  5. this.data = data;
  6. }
  7. } // end of Node5

  8. public class ReverseLinkedList {

  9. Node5 head;

  10. public static void main(String[] args) {
  11. ReverseLinkedList l = new ReverseLinkedList();
  12. l.add(3);
  13. l.add(5);
  14. l.add(8);
  15. l.add(9);
  16. l.print(l.head);
  17. l.reverse();
  18. System.out.println("\nafter reverse linked list");
  19. l.print(l.head);
  20. } // end of main()

  21. // add the element in LinkedList
  22. public void add(int data) {
  23. Node5 next = new Node5(data);
  24. if (head == null) {
  25. head = next;
  26. return;
  27. }
  28. Node5 last = head;
  29. while (last.next != null) {
  30. last = last.next;
  31. }
  32. last.next = next;
  33. } // end of add()

  34. // print the LinkedList
  35. void print(Node5 next) {
  36. Node5 temp = next;
  37. while (temp != null) {
  38. System.out.print(temp.data + "-->");
  39. temp = temp.next;
  40. }
  41. } // end of print()

  42. // reverse the LinkedList
  43. Node5 reverse() {
  44. Node5 prev = null;
  45. Node5 current = head;
  46. Node5 next = null;
  47. while (current != null) {
  48. next = current.next;
  49. current.next = prev;
  50. prev = current;
  51. current = next;
  52. }
  53. head = prev;
  54. return head;
  55. } // end of reverse()
  56. } //end of ReverseLinkedList 
Output: 
3-->5-->8-->9-->
after reverse linked list
9-->8-->5-->3-->

Reverse a string array !!

Problem statement: Reverse a string array in java !
Using for Loop:
  1. public class ReverseStringArrayExe1 {
  2. public static void main(String[] args) {

  3. String a[] = { "apples", "tomatoes", "bananas", "guavas", "pineapples" };
  4. for (int i = 0; i < a.length / 2; i++) {
  5. Object temp = a[i];
  6. a[i] = a[a.length - 1 - i];
  7. a[a.length - 1 - i] = (String) temp;

  8. }
  9. for (String s : a) {
  10. System.out.print(s + " ");

  11. }
  12. }
  13. }
Output: 
pineapples guavas bananas tomatoes apples
Time complexity: O(n/2)

Using while Loop:
  1. public class ReverseStringArray {
  2. public static void main(String[] args) {
  3. String a[] = { "apples", "tomatoes", "bananas", "guavas", "pineapples" };
  4. int end = a.length - 1;
  5. int start = 0;
  6. String temp;
  7. while (start < end) {
  8. temp = a[start];
  9. a[start] = a[end];
  10. a[end] = temp;
  11. start++;
  12. end--;
  13. }
  14. for (String i : a) {
  15. System.out.print(i + " ");
  16. }
  17. }
  18. }
Output:
pineapples guavas bananas tomatoes apples
Time complexity: O(n/2)

Using Java 8 Stream API:
  1. import java.util.stream.IntStream;

  2. public class ReverseArrayUsingStream {
  3. public static void main(String[] args) {
  4. String a[] = { "apples", "tomatoes", "bananas", "guavas", "pineapples" };
  5. reverseUsingStream(a);
  6. }

  7. static void reverseUsingStream(Object a[]) {

  8. Object rev[] =IntStream.rangeClosed(1, a.length).mapToObj(i -> a[a.length - i]).toArray();
  9. for (Object s : rev) {
  10. System.out.print(s + " ");
  11. }
  12. }
  13. }
Output:
pineapples guavas bananas tomatoes apples 

Using Collections.reverse() :
  1. import java.util.List;
  2. import java.util.Arrays;
  3. import java.util.Collections;

  4. public class ReverseArrayCollections {
  5. public static void main(String[] args) {
  6. String a[] = { "apples", "tomatoes", "bananas", "guavas", "pineapples" };
  7. reverseUsingCollectionsUtils(a);
  8. }
  9. static void reverseUsingCollectionsUtils(Object[] array) {
  10. List<Object> list = Arrays.asList(array);
  11. Collections.reverse(list);
  12. for (Object i : list) {
  13. System.out.print(i + " ");
  14. }
  15. }
  16. }
Output:
pineapples guavas bananas tomatoes apples

Reverse an integer array !!

Problem statement:  Reverse an integer array in java !
Using  for Loop:
  1. public class ReverseNumberArray {
  2. public static void main(String[] args) {
  3. int a[] = { 2, 4, 6, 7, 3, 56, 34 };
  4. for (int i = 0; i < a.length / 2; i++) {
  5. int temp = a[i];
  6. a[i] = a[a.length - 1 - i];
  7. a[a.length - 1 - i] = temp;
  8. }
  9. for (int i : a) {
  10. System.out.print(i + " ");
  11. }
  12. }
  13. }
Output: 
34 56 3 7 6 4 2 
Time complexity: O(n/2)

Using while Loop:
  1. public class ReverseNumberArray {

  2. public static void main(String[] args) {
  3. int a[] = { 2, 4, 6, 7, 3, 56, 34 };

  4. int end = a.length - 1;
  5. int start = 0;
  6. Object temp;
  7. while (start < end) {
  8. temp = a[start];
  9. a[start] = a[end];
  10. a[end] = (int) temp;
  11. start++;
  12. end--;
  13. }
  14. for (int i : a) {
  15. System.out.print(i + " ");
  16. }
  17. }
  18. }
Output: 
34 56 3 7 6 4 2 
Time complexity: O(n/2)

Using Java 8 Stream API:
  1. import java.util.stream.IntStream;

  2. public class ReverseArrayUsingStream {
  3. public static void main(String[] args) {
  4. Integer a[] = { 2, 4, 6, 7, 3, 56, 34 };
  5. reverseUsingStream(a);
  6. }

  7. static void reverseUsingStream(Object a[]) {

  8. Object rev[] = IntStream.rangeClosed(1, a.length).mapToObj(i -> a[a.length - i]).toArray();
  9. for (Object s : rev) {
  10. System.out.print(s + " ");
  11. }
  12. }
  13. }
Using Collections.reverse():

  1. import java.util.List;
  2. import java.util.Arrays;
  3. import java.util.Collections;

  4. public class ReverseArrayCollections {
  5. public static void main(String[] args) {

  6. Object a[] = { 2, 4, 6, 7, 3, 56, 34 };
  7. reverseUsingCollectionsUtils(a);
  8. }
  9. static void reverseUsingCollectionsUtils(Object[] array) {
  10. List<Object> list = Arrays.asList(array);
  11. Collections.reverse(list);
  12. for (Object i : list) {
  13. System.out.print(i + " ");
  14. }
  15. }
  16. }

Output:
34 56 3 7 6 4 2 

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...