In: Computer Science
import java.io.*;
import java.util.Scanner;
class Node {
int data;
Node next;
Node(int d){ // Constructor
data = d;
next = null;
}
}
class ACOLinkedList {// a Singly Linked List
Node head; // head of list
public void insert(int data){ // Method to insert a
new node
Node new_node = new Node(data); //
Create a new node with given data
new_node.next = null;
if (head == null) // If the Linked
List is empty, then make the new node as head
head =
new_node;
else {// Else traverse till the
last node and insert the new_node there
Node last =
head;
while (last.next
!= null)
last = last.next;
last.next =
new_node; // Insert the new_node at last node
}
}
}
class Main {
public static void main(String[] args)
{
ACOLinkedList list = new
ACOLinkedList();/* Start with the empty list. */
Scanner scan = new
Scanner(System.in);
int num;
for (int i=0; i<10; i++){//Read
list values
num =
scan.nextInt();
list.insert(num);
}
System.out.println(""+getSecondMax(list));
}
public static int getSecondMax(ACOLinkedList list) {
//TODO: Find the second largest
element in the list (list could have any number of elements)
//Example: if list={40, 20, 25, 15,
99}, the method should return: 40
}
}
METHOD IN JAVA :
public static int getSecondMax(ACOLinkedList list) { ACOLinkedList temp=list; Node t=temp.head; int max1= Integer.MIN_VALUE; int max2= Integer.MIN_VALUE; while(t!=null) { if(t.data>max1) { max2=max1; max1=t.data; } else if(t.data>max2&&t.data!=max1) max2=t.data; t=t.next; } // to indicate there is no second largest element present , we returns -1 if(max2== Integer.MIN_VALUE) return -1; //else return the second largest element else return max2; }
WHOLE CODE TOGETHER :
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor data = d; next = null; } } class ACOLinkedList {// a Singly Linked List Node head; // head of list public void insert(int data){ // Method to insert a new node Node new_node = new Node(data); // Create a new node with given data new_node.next = null; if (head == null) // If the Linked List is empty, then make the new node as head head = new_node; else {// Else traverse till the last node and insert the new_node there Node last = head; while (last.next != null) last = last.next; last.next = new_node; // Insert the new_node at last node } } } public class Main { public static void main(String[] args) { ACOLinkedList list = new ACOLinkedList();/* Start with the empty list. */ Scanner scan = new Scanner(System.in); int num; for (int i = 0; i < 10; i++) {//Read list values num = scan.nextInt(); list.insert(num); } System.out.println("" + getSecondMax(list)); } public static int getSecondMax(ACOLinkedList list) { ACOLinkedList temp=list; Node t=temp.head; int max1= Integer.MIN_VALUE; int max2= Integer.MIN_VALUE; while(t!=null) { if(t.data>max1) { max2=max1; max1=t.data; } else if(t.data>max2&&t.data!=max1) max2=t.data; t=t.next; } // to indicate there is no second largest element present , we returns -1 if(max2== Integer.MIN_VALUE) return -1; //else return the second largest element else return max2; } }
EXAMPLE OF ABOVE CODE :