Question

In: Computer Science

Java coding: 2. Write a method which takes a list list of int , and reverse...

Java coding:

2. Write a method which takes a list list of int , and reverse it. // recursion

3.Write a method which takes a list list of strings , and reverse it. // in different way than the previous

3. Write a two methods which take a list and find the largest integer number in it.

Solutions

Expert Solution

2. Java_program for reversing the linked_list

class LinkedList {

   static Node head;

   static class Node {

       int data;
       Node next;

       Node(int d)
       {
           data = d;
           next = null;
       }
   }

   /* Function to reverse_linked_list */
   Node reverse(Node node)
   {
       Node prev = null;
       Node current = node;
       Node next = null;
       while (current != null) {
           next = current.next;
           current.next = prev;
           prev = current;
           current = next;
       }
       node = prev;
       return node;
   }

   // prints_content of double_linked_list
   void print List(Node node)
   {
       while (node != null) {
           System.out.print(node.data + " ");
           node = node.next;
       }
   }

   public static void main(String[] args)
   {
       LinkedList list = new LinkedList();
       list.head = new Node(65);
       list.head.next = new Node(25);
       list.head.next.next = new Node(14);
       list.head.next.next.next = new Node(20);

       System.out.println("Given Linked_list");
       list.printList(head);
       head = list.reverse(head);
       System.out.println("");
       System.out.println("Reversed linked_list ");
       list.printList(head);
   }
}

Output:
Given linked_list
65 25 14 20 
Reversed Linked_list 
20 14 25 65 

3. Take a list of strings , reverse it.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class Main
{
public static void main(String[] args)
{
List<String> colors = new ArrayList<>(
Arrays.asList("GREEN", "YELLOW", "BLACK")); // INITIALIZE THE LIST

Collections.reverse(colors);
System.out.println(colors);
}
}

output: BLACK YELLOW GREEN

3. find the largest integer number from a list in java

Method_1: Iterative_Way

// Java Program to find largest integer number from a list
class Test
{
   static int arr[] = {100, 124, 145, 190, 808};
  
   // Method to find largest integer number from a list
   static int largest()
   {
       int i;
      
       // Initialize max integer
       int max = arr[0];
      
       // Traverse list (array) elements from second and
       // compare every element with current max
       for (i = 1; i < arr.length; i++)
           if (arr[i] > max)
               max = arr[i];
      
       return max;
   }
  
   // Driver_method
   public static void main(String[] args)
   {
       System.out.println("Largest in list is " + largest());
       }
}

Output:

Largest in list is 808

Method 2 : Sorting List

// Java Program to find largest integer number from a list
// arr[] of size n
import java .io.*;
import java.util.*;
  
class GFG
{     
   // returns maximum number from a list
   static int largest(int []arr,     
                   int n)
   {
       Arrays.sort(arr);
       return arr[n - 1];
   }
  
   // Driver code
   static public void main (String[] args)
   {
       int []arr = {10, 24, 45,
                   90, 980};
       int n = arr.length;
       System.out.println(largest(arr, n));
   }
}

Output: 980


Related Solutions

(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Just that method --> Java code Write a method “int sumPos(List aList)” to calculate the sum...
Just that method --> Java code Write a method “int sumPos(List aList)” to calculate the sum of positive integers in an ADT List aList of integers using ADT List operations. ADT List operations: isEmpty(), size(), add(index, item), remove(index), get(index), and removeAll(). You should not assume how an ADT List is implemented. Using array indexing, head/tail references, or any operation not defined in ADT List is not allowed.
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
Which row has the largest sum? Write a method that takes a 2D int array and...
Which row has the largest sum? Write a method that takes a 2D int array and prints: of The index of the row that has the largest sum The sum of elements in that row java
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the addPlayers(int players) method * Complete the passPotatoe(int passes) method * No other methods/variables should be added/modified */ public class A3CircleLL {    /*    * Grading:    * Correctly uses helpers to play game - 1pt    * Prints correct winner when game is complete - 0.5pt    */    public void playGame(int players, int passes) {        /*        * Use...
Please write in Java and have two methods: the main method and the reverse word Write...
Please write in Java and have two methods: the main method and the reverse word Write a method that reads a line and reverses the words in the line (not the characters) using a stack. For example, given the following input: The quick brown fox jumps over the lazy dog you should get the following output: dog lazy the over jumps fox brown quick The Then create a main method to prompt the user to enter a line of words...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT