# Find the largest and second largest node in a linked list

Here given code implementation process.

``````// C Program
// Find the largest and second largest node in a linked list
#include <stdio.h>
//For malloc function
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
};
//Create a node of linked list
struct Node *create_node(int data)
{
//Create dynamic node
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
//Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
void insert(struct Node **head, int data)
{
struct Node *node = create_node(data);
{
}
else
{
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
}
}
{
{
return;
}
while (temp != NULL)
{
printf("  %d", temp->data);
//visit to next node
temp = temp->next;
}
}
//Find the largest and second largest element in linked list
{
{
return;
}
//Define some auxiliary variables
struct Node *big = NULL;
struct Node *second_big = NULL;
while (temp != NULL)
{
if (big == NULL)
{
//Get first node
big = temp;
}
else if (big->data < temp->data)
{
//Get a new big node
//Get previous largest node
second_big = big;
//Get current largest node
big = temp;
}
else if (big->data != temp->data)
{
if (second_big == NULL)
{
//If in case second largest node empty
second_big = temp;
}
else if (second_big->data < temp->data)
{
//Get new second largest node
second_big = temp;
}
}
//visit to next node
temp = temp->next;
}
if (big != NULL)
{
printf("\n Largest  :  %d\n", big->data);
}
if (second_big != NULL)
{
printf(" Second largest : %d \n", second_big->data);
}
else
{
//When second largest node are not exist in linked list
printf(" Second largest : None\n");
}
}
int main()
{
struct Node *list1 = NULL;
struct Node *list2 = NULL;
struct Node *list3 = NULL;
struct Node *list4 = NULL;
insert( &list1, 6);
insert( &list1, 4);
insert( &list1, 5);
insert( &list1, 10);
insert( &list1, 3);
insert( &list1, 7);
insert( &list1, 9);
insert( &list1, 2);
insert( &list1, 8);
largest_2nd_largest(list1);
insert( &list2, 1);
insert( &list2, 1);
insert( &list2, 4);
insert( &list2, 2);
insert( &list2, 3);
insert( &list2, 1);
largest_2nd_largest(list2);
insert( &list3, 2);
insert( &list3, 6);
insert( &list3, 8);
insert( &list3, 4);
insert( &list3, 8);
insert( &list3, 3);
insert( &list3, 3);
insert( &list3, 2);
largest_2nd_largest(list3);
insert( &list4, 2);
insert( &list4, 2);
insert( &list4, 2);
largest_2nd_largest(list4);
return 0;
}``````

#### Output

`````` Linked List :   6  4  5  10  3  7  9  2  8
Largest  :  10
Second largest : 9

Linked List :   1  1  4  2  3  1
Largest  :  4
Second largest : 3

Linked List :   2  6  8  4  8  3  3  2
Largest  :  8
Second largest : 6

Linked List :   2  2  2
Largest  :  2
Second largest : None``````
``````// Java Program
// Find the largest and second largest node in a linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructor
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
//display node value
System.out.print("  " + temp.data);
//visit to next node
temp = temp.next;
}
}
//Find the largest and second largest element in linked list
public void largest_2nd_largest()
{
{
return;
}
this.display();
//Define some auxiliary variables
Node big = null;
Node second_big = null;
while (temp != null)
{
if (big == null)
{
//Get first node
big = temp;
}
else if (big.data < temp.data)
{
//Get a new big node
//Get previous largest node
second_big = big;
//Get current largest node
big = temp;
}
else if (big.data != temp.data)
{
if (second_big == null)
{
//If in case second largest node empty
second_big = temp;
}
else if (second_big.data < temp.data)
{
//Get new second largest node
second_big = temp;
}
}
//visit to next node
temp = temp.next;
}
if (big != null)
{
System.out.print("\n Largest : " + big.data + "\n");
}
if (second_big != null)
{
System.out.print(" Second largest : " + second_big.data + " \n");
}
else
{
//When second largest node are not exist in linked list
System.out.print(" Second largest : None\n");
}
}
public static void main(String[] args)
{
list1.insert(6);
list1.insert(4);
list1.insert(5);
list1.insert(10);
list1.insert(3);
list1.insert(7);
list1.insert(9);
list1.insert(2);
list1.insert(8);
list1.largest_2nd_largest();
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
list2.largest_2nd_largest();
list3.insert(2);
list3.insert(6);
list3.insert(8);
list3.insert(4);
list3.insert(8);
list3.insert(3);
list3.insert(3);
list3.insert(2);
list3.largest_2nd_largest();
list4.insert(2);
list4.insert(2);
list4.insert(2);
list4.largest_2nd_largest();
}
}``````

#### Output

`````` Linked List :   6  4  5  10  3  7  9  2  8
Largest : 10
Second largest : 9

Linked List :   1  1  4  2  3  1
Largest : 4
Second largest : 3

Linked List :   2  6  8  4  8  3  3  2
Largest : 8
Second largest : 6

Linked List :   2  2  2
Largest : 2
Second largest : None``````
``````//Include header file
#include <iostream>

using namespace std;
// C++ Program
// Find the largest and second largest node in a linked list

class Node
{
public:
int data;
Node * next;
Node(int data)
{
//Set node value
this->data = data;
this->next = NULL;
}
};
{
Node * tail;
//Class constructor
{
this->tail = NULL;
}
//insert node at last of linke list
void insert(int data)
{
//Create a node
Node * node = new Node(data);
{
this->tail = node;
}
else
{
this->tail->next = node;
this->tail = node;
}
}
void display()
{
{
return;
}
cout << "\n Linked List : ";
while (temp != NULL)
{
//display node value
cout << "  " << temp->data;
//visit to next node
temp = temp->next;
}
}
//Find the largest and second largest element in linked list
void largest_2nd_largest()
{
{
return;
}
this->display();
//Define some auxiliary variables
Node * big = NULL;
Node * second_big = NULL;
while (temp != NULL)
{
if (big == NULL)
{
//Get first node
big = temp;
}
else if (big->data < temp->data)
{
//Get a new big node
//Get previous largest node
second_big = big;
//Get current largest node
big = temp;
}
else if (big->data != temp->data)
{
if (second_big == NULL)
{
//If in case second largest node empty
second_big = temp;
}
else if (second_big->data < temp->data)
{
//Get new second largest node
second_big = temp;
}
}
//visit to next node
temp = temp->next;
}
if (big != NULL)
{
cout << "\n Largest : " << big->data << "\n";
}
if (second_big != NULL)
{
cout << " Second largest : " << second_big->data << " \n";
}
else
{
//When second largest node are not exist in linked list
cout << " Second largest : None\n";
}
}
};
int main()
{
list1.insert(6);
list1.insert(4);
list1.insert(5);
list1.insert(10);
list1.insert(3);
list1.insert(7);
list1.insert(9);
list1.insert(2);
list1.insert(8);
list1.largest_2nd_largest();
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
list2.largest_2nd_largest();
list3.insert(2);
list3.insert(6);
list3.insert(8);
list3.insert(4);
list3.insert(8);
list3.insert(3);
list3.insert(3);
list3.insert(2);
list3.largest_2nd_largest();
list4.insert(2);
list4.insert(2);
list4.insert(2);
list4.largest_2nd_largest();
return 0;
}``````

#### Output

`````` Linked List :   6  4  5  10  3  7  9  2  8
Largest : 10
Second largest : 9

Linked List :   1  1  4  2  3  1
Largest : 4
Second largest : 3

Linked List :   2  6  8  4  8  3  3  2
Largest : 8
Second largest : 6

Linked List :   2  2  2
Largest : 2
Second largest : None``````
``````//Include namespace system
using System;
// C# Program
// Find the largest and second largest node in a linked list

public class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructor
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
//display node value
Console.Write("  " + temp.data);
//visit to next node
temp = temp.next;
}
}
//Find the largest and second largest element in linked list
public void largest_2nd_largest()
{
{
return;
}
this.display();
//Define some auxiliary variables
Node big = null;
Node second_big = null;
while (temp != null)
{
if (big == null)
{
//Get first node
big = temp;
}
else if (big.data < temp.data)
{
//Get a new big node
//Get previous largest node
second_big = big;
//Get current largest node
big = temp;
}
else if (big.data != temp.data)
{
if (second_big == null)
{
//If in case second largest node empty
second_big = temp;
}
else if (second_big.data < temp.data)
{
//Get new second largest node
second_big = temp;
}
}
//visit to next node
temp = temp.next;
}
if (big != null)
{
Console.Write("\n Largest : " + big.data + "\n");
}
if (second_big != null)
{
Console.Write(" Second largest : " + second_big.data + " \n");
}
else
{
//When second largest node are not exist in linked list
Console.Write(" Second largest : None\n");
}
}
public static void Main(String[] args)
{
list1.insert(6);
list1.insert(4);
list1.insert(5);
list1.insert(10);
list1.insert(3);
list1.insert(7);
list1.insert(9);
list1.insert(2);
list1.insert(8);
list1.largest_2nd_largest();
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
list2.largest_2nd_largest();
list3.insert(2);
list3.insert(6);
list3.insert(8);
list3.insert(4);
list3.insert(8);
list3.insert(3);
list3.insert(3);
list3.insert(2);
list3.largest_2nd_largest();
list4.insert(2);
list4.insert(2);
list4.insert(2);
list4.largest_2nd_largest();
}
}``````

#### Output

`````` Linked List :   6  4  5  10  3  7  9  2  8
Largest : 10
Second largest : 9

Linked List :   1  1  4  2  3  1
Largest : 4
Second largest : 3

Linked List :   2  6  8  4  8  3  3  2
Largest : 8
Second largest : 6

Linked List :   2  2  2
Largest : 2
Second largest : None``````
``````<?php
// Php Program
// Find the largest and second largest node in a linked list

class Node
{
public \$data;
public \$next;

function __construct(\$data)
{
//Set node value
\$this->data = \$data;
\$this->next = null;
}
}
{
public \$tail;
//Class constructor
function __construct()
{
\$this->tail = null;
}
//insert node at last of linke list
public	function insert(\$data)
{
//Create a node
\$node = new Node(\$data);
{
\$this->tail = \$node;
}
else
{
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
public	function display()
{
{
return;
}
echo "\n Linked List : ";
while (\$temp != null)
{
//display node value
echo "  ". \$temp->data;
//visit to next node
\$temp = \$temp->next;
}
}
//Find the largest and second largest element in linked list
public	function largest_2nd_largest()
{
{
return;
}
\$this->display();
//Define some auxiliary variables
\$big = null;
\$second_big = null;
while (\$temp != null)
{
if (\$big == null)
{
//Get first node
\$big = \$temp;
}
else if (\$big->data < \$temp->data)
{
//Get a new big node
//Get previous largest node
\$second_big = \$big;
//Get current largest node
\$big = \$temp;
}
else if (\$big->data != \$temp->data)
{
if (\$second_big == null)
{
//If in case second largest node empty
\$second_big = \$temp;
}
else if (\$second_big->data < \$temp->data)
{
//Get new second largest node
\$second_big = \$temp;
}
}
//visit to next node
\$temp = \$temp->next;
}
if (\$big != null)
{
echo "\n Largest : ". \$big->data ."\n";
}
if (\$second_big != null)
{
echo " Second largest : ". \$second_big->data ." \n";
}
else
{
//When second largest node are not exist in linked list
echo " Second largest : None\n";
}
}
}

function main()
{
\$list1->insert(6);
\$list1->insert(4);
\$list1->insert(5);
\$list1->insert(10);
\$list1->insert(3);
\$list1->insert(7);
\$list1->insert(9);
\$list1->insert(2);
\$list1->insert(8);
\$list1->largest_2nd_largest();
\$list2->insert(1);
\$list2->insert(1);
\$list2->insert(4);
\$list2->insert(2);
\$list2->insert(3);
\$list2->insert(1);
\$list2->largest_2nd_largest();
\$list3->insert(2);
\$list3->insert(6);
\$list3->insert(8);
\$list3->insert(4);
\$list3->insert(8);
\$list3->insert(3);
\$list3->insert(3);
\$list3->insert(2);
\$list3->largest_2nd_largest();
\$list4->insert(2);
\$list4->insert(2);
\$list4->insert(2);
\$list4->largest_2nd_largest();
}
main();``````

#### Output

`````` Linked List :   6  4  5  10  3  7  9  2  8
Largest : 10
Second largest : 9

Linked List :   1  1  4  2  3  1
Largest : 4
Second largest : 3

Linked List :   2  6  8  4  8  3  3  2
Largest : 8
Second largest : 6

Linked List :   2  2  2
Largest : 2
Second largest : None``````
``````// Node Js Program
// Find the largest and second largest node in a linked list

class Node
{
constructor(data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
//Class constructor
constructor()
{
this.tail = null;
}
//insert node at last of linke list
insert(data)
{
//Create a node
var node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
display()
{
{
return;
}
while (temp != null)
{
//display node value
process.stdout.write("  " + temp.data);
//visit to next node
temp = temp.next;
}
}
//Find the largest and second largest element in linked list
largest_2nd_largest()
{
{
return;
}
this.display();
//Define some auxiliary variables
var big = null;
var second_big = null;
while (temp != null)
{
if (big == null)
{
//Get first node
big = temp;
}
else if (big.data < temp.data)
{
//Get a new big node
//Get previous largest node
second_big = big;
//Get current largest node
big = temp;
}
else if (big.data != temp.data)
{
if (second_big == null)
{
//If in case second largest node empty
second_big = temp;
}
else if (second_big.data < temp.data)
{
//Get new second largest node
second_big = temp;
}
}
//visit to next node
temp = temp.next;
}
if (big != null)
{
process.stdout.write("\n Largest : " + big.data + "\n");
}
if (second_big != null)
{
process.stdout.write(" Second largest : " + second_big.data + " \n");
}
else
{
//When second largest node are not exist in linked list
process.stdout.write(" Second largest : None\n");
}
}
}

function main()
{
list1.insert(6);
list1.insert(4);
list1.insert(5);
list1.insert(10);
list1.insert(3);
list1.insert(7);
list1.insert(9);
list1.insert(2);
list1.insert(8);
list1.largest_2nd_largest();
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
list2.largest_2nd_largest();
list3.insert(2);
list3.insert(6);
list3.insert(8);
list3.insert(4);
list3.insert(8);
list3.insert(3);
list3.insert(3);
list3.insert(2);
list3.largest_2nd_largest();
list4.insert(2);
list4.insert(2);
list4.insert(2);
list4.largest_2nd_largest();
}
main();``````

#### Output

`````` Linked List :   6  4  5  10  3  7  9  2  8
Largest : 10
Second largest : 9

Linked List :   1  1  4  2  3  1
Largest : 4
Second largest : 3

Linked List :   2  6  8  4  8  3  3  2
Largest : 8
Second largest : 6

Linked List :   2  2  2
Largest : 2
Second largest : None``````
``````#  Python 3 Program
#  Find the largest and second largest node in a linked list

class Node :

def __init__(self, data) :
# Set node value
self.data = data
self.next = None

# Class constructor
def __init__(self) :
self.tail = None

# insert node at last of linke list
def insert(self, data) :
# Create a node
node = Node(data)
self.tail = node
else :
self.tail.next = node
self.tail = node

def display(self) :
print("\nEmpty linked list\n", end = "")
return

print("\n Linked List : ", end = "")
while (temp != None) :
# display node value
print("  ", temp.data, end = "")
# visit to next node
temp = temp.next

# Find the largest and second largest element in linked list
def largest_2nd_largest(self) :
print("\nEmpty linked list\n", end = "")
return

self.display()
# Define some auxiliary variables
big = None
second_big = None
while (temp != None) :
if (big == None) :
# Get first node
big = temp

elif(big.data < temp.data) :
# Get a new big node
# Get previous largest node
second_big = big
# Get current largest node
big = temp

elif(big.data != temp.data) :
if (second_big == None) :
# If in case second largest node empty
second_big = temp

elif(second_big.data < temp.data) :
# Get new second largest node
second_big = temp

# visit to next node
temp = temp.next

if (big != None) :
print("\n Largest : ", big.data ,"\n", end = "")

if (second_big != None) :
print(" Second largest : ", second_big.data ," \n", end = "")
else :
# When second largest node are not exist in linked list
print(" Second largest : None\n", end = "")

def main() :
list1.insert(6)
list1.insert(4)
list1.insert(5)
list1.insert(10)
list1.insert(3)
list1.insert(7)
list1.insert(9)
list1.insert(2)
list1.insert(8)
list1.largest_2nd_largest()
list2.insert(1)
list2.insert(1)
list2.insert(4)
list2.insert(2)
list2.insert(3)
list2.insert(1)
list2.largest_2nd_largest()
list3.insert(2)
list3.insert(6)
list3.insert(8)
list3.insert(4)
list3.insert(8)
list3.insert(3)
list3.insert(3)
list3.insert(2)
list3.largest_2nd_largest()
list4.insert(2)
list4.insert(2)
list4.insert(2)
list4.largest_2nd_largest()

if __name__ == "__main__": main()``````

#### Output

`````` Linked List :    6   4   5   10   3   7   9   2   8
Largest :  10
Second largest :  9

Linked List :    1   1   4   2   3   1
Largest :  4
Second largest :  3

Linked List :    2   6   8   4   8   3   3   2
Largest :  8
Second largest :  6

Linked List :    2   2   2
Largest :  2
Second largest : None``````
``````#  Ruby Program
#  Find the largest and second largest node in a linked list

class Node

# Define the accessor and reader of class Node
attr_accessor :data, :next

def initialize(data)

# Set node value
self.data = data
self.next = nil
end
end

# Class constructor
def initialize()

self.tail = nil
end
# insert node at last of linke list
def insert(data)

# Create a node
node = Node.new(data)

self.tail = node
else

self.tail.next = node
self.tail = node
end
end
def display()

return
end
while (temp != nil)

# display node value
print("  ", temp.data)
# visit to next node
temp = temp.next
end
end
# Find the largest and second largest element in linked list
def largest_2nd_largest()

return
end
self.display()
# Define some auxiliary variables
big = nil
second_big = nil
while (temp != nil)

if (big == nil)

# Get first node
big = temp
elsif(big.data < temp.data)

# Get a new big node
# Get previous largest node
second_big = big
# Get current largest node
big = temp
elsif(big.data != temp.data)

if (second_big == nil)

# If in case second largest node empty
second_big = temp
elsif(second_big.data < temp.data)

# Get new second largest node
second_big = temp
end
end
# visit to next node
temp = temp.next
end
if (big != nil)

print("\n Largest : ", big.data ,"\n")
end
if (second_big != nil)

print(" Second largest : ", second_big.data ," \n")
else

# When second largest node are not exist in linked list
print(" Second largest : None\n")
end
end
end
def main()

list1.insert(6)
list1.insert(4)
list1.insert(5)
list1.insert(10)
list1.insert(3)
list1.insert(7)
list1.insert(9)
list1.insert(2)
list1.insert(8)
list1.largest_2nd_largest()
list2.insert(1)
list2.insert(1)
list2.insert(4)
list2.insert(2)
list2.insert(3)
list2.insert(1)
list2.largest_2nd_largest()
list3.insert(2)
list3.insert(6)
list3.insert(8)
list3.insert(4)
list3.insert(8)
list3.insert(3)
list3.insert(3)
list3.insert(2)
list3.largest_2nd_largest()
list4.insert(2)
list4.insert(2)
list4.insert(2)
list4.largest_2nd_largest()
end
main()``````

#### Output

`````` Linked List :   6  4  5  10  3  7  9  2  8
Largest : 10
Second largest : 9

Linked List :   1  1  4  2  3  1
Largest : 4
Second largest : 3

Linked List :   2  6  8  4  8  3  3  2
Largest : 8
Second largest : 6

Linked List :   2  2  2
Largest : 2
Second largest : None
``````
``````// Scala Program
// Find the largest and second largest node in a linked list

class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
var tail: Node)
{
//Class constructor
def this()
{
this(null, null);
}
//insert node at last of linke list
def insert(data: Int): Unit = {
//Create a node
var node: Node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
def display(): Unit = {
{
return;
}
while (temp != null)
{
//display node value
print("  " + temp.data);
//visit to next node
temp = temp.next;
}
}
//Find the largest and second largest element in linked list
def largest_2nd_largest(): Unit = {
{
return;
}
this.display();
//Define some auxiliary variables
var big: Node = null;
var second_big: Node = null;
while (temp != null)
{
if (big == null)
{
//Get first node
big = temp;
}
else if (big.data < temp.data)
{
//Get a new big node
//Get previous largest node
second_big = big;
//Get current largest node
big = temp;
}
else if (big.data != temp.data)
{
if (second_big == null)
{
//If in case second largest node empty
second_big = temp;
}
else if (second_big.data < temp.data)
{
//Get new second largest node
second_big = temp;
}
}
//visit to next node
temp = temp.next;
}
if (big != null)
{
print("\n Largest : " + big.data + "\n");
}
if (second_big != null)
{
print(" Second largest : " + second_big.data + " \n");
}
else
{
//When second largest node are not exist in linked list
print(" Second largest : None\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
list1.insert(6);
list1.insert(4);
list1.insert(5);
list1.insert(10);
list1.insert(3);
list1.insert(7);
list1.insert(9);
list1.insert(2);
list1.insert(8);
list1.largest_2nd_largest();
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
list2.largest_2nd_largest();
list3.insert(2);
list3.insert(6);
list3.insert(8);
list3.insert(4);
list3.insert(8);
list3.insert(3);
list3.insert(3);
list3.insert(2);
list3.largest_2nd_largest();
list4.insert(2);
list4.insert(2);
list4.insert(2);
list4.largest_2nd_largest();
}
}``````

#### Output

`````` Linked List :   6  4  5  10  3  7  9  2  8
Largest : 10
Second largest : 9

Linked List :   1  1  4  2  3  1
Largest : 4
Second largest : 3

Linked List :   2  6  8  4  8  3  3  2
Largest : 8
Second largest : 6

Linked List :   2  2  2
Largest : 2
Second largest : None``````
``````// Swift Program
// Find the largest and second largest node in a linked list

class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int)
{
//Set node value
self.data = data;
self.next = nil;
}
}
{
var tail: Node? ;
//Class constructor
init()
{
self.tail = nil;
}
//insert node at last of linke list
func insert(_ data: Int)
{
//Create a node
let node: Node? = Node(data);
{
self.tail = node;
}
else
{
self.tail!.next = node;
self.tail = node;
}
}
func display()
{
{
return;
}
print("\n Linked List : ", terminator: "");
while (temp != nil)
{
//display node value
print("  ", temp!.data, terminator: "");
//visit to next node
temp = temp!.next;
}
}
//Find the largest and second largest element in linked list
func largest_2nd_largest()
{
{
return;
}
self.display();
//Define some auxiliary variables
var big: Node? = nil;
var second_big: Node? = nil;
while (temp != nil)
{
if (big == nil)
{
//Get first node
big = temp;
}
else if (big!.data < temp!.data)
{
//Get a new big node
//Get previous largest node
second_big = big;
//Get current largest node
big = temp;
}
else if (big!.data != temp!.data)
{
if (second_big == nil)
{
//If in case second largest node empty
second_big = temp;
}
else if (second_big!.data < temp!.data)
{
//Get new second largest node
second_big = temp;
}
}
//visit to next node
temp = temp!.next;
}
if (big != nil)
{
print("\n Largest : ", big!.data ,"\n", terminator: "");
}
if (second_big != nil)
{
print(" Second largest : ", second_big!.data ," \n", terminator: "");
}
else
{
//When second largest node are not exist in linked list
print(" Second largest : None\n", terminator: "");
}
}
}
func main()
{
list1.insert(6);
list1.insert(4);
list1.insert(5);
list1.insert(10);
list1.insert(3);
list1.insert(7);
list1.insert(9);
list1.insert(2);
list1.insert(8);
list1.largest_2nd_largest();
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
list2.largest_2nd_largest();
list3.insert(2);
list3.insert(6);
list3.insert(8);
list3.insert(4);
list3.insert(8);
list3.insert(3);
list3.insert(3);
list3.insert(2);
list3.largest_2nd_largest();
list4.insert(2);
list4.insert(2);
list4.insert(2);
list4.largest_2nd_largest();
}
main();``````

#### Output

`````` Linked List :    6   4   5   10   3   7   9   2   8
Largest :  10
Second largest :  9

Linked List :    1   1   4   2   3   1
Largest :  4
Second largest :  3

Linked List :    2   6   8   4   8   3   3   2
Largest :  8
Second largest :  6

Linked List :    2   2   2
Largest :  2
Second largest : None``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.