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
}
}
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)
{
Node trav = list.head;
int max = -1, secondMax = -2;
while (trav != null) {
if (max <
trav.data) {
secondMax = max;
max = trav.data;
} else if
(secondMax < trav.data) {
secondMax = trav.data;
}
trav =
trav.next;
}
return secondMax;
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME