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>
//Linked List Node
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;
}
//Add new node at end of linked list
void insert(struct Node **head, int data)
{
struct Node *node = create_node(data);
if ( *head == NULL)
{
*head = node;
}
else
{
struct Node *temp = *head;
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
//Add node at last possition
temp->next = node;
}
}
//Display linked list element
void display(struct Node *head)
{
if (head == NULL)
{
printf("\nEmpty linked list\n");
return;
}
struct Node *temp = head;
printf("\n Linked List : ");
//iterating linked list elements
while (temp != NULL)
{
printf(" %d", temp->data);
//visit to next node
temp = temp->next;
}
}
//Find the largest and second largest element in linked list
void largest_2nd_largest(struct Node *head)
{
if (head == NULL)
{
printf("\nEmpty linked list\n");
return;
}
//Display linked list
display(head);
//Define some auxiliary variables
struct Node *big = NULL;
struct Node *second_big = NULL;
struct Node *temp = head;
//iterating linked list elements
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()
{
//Define few empty linked list
struct Node *list1 = NULL;
struct Node *list2 = NULL;
struct Node *list3 = NULL;
struct Node *list4 = NULL;
//Add node in first linked list
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);
//Add node in second linked list
insert( &list2, 1);
insert( &list2, 1);
insert( &list2, 4);
insert( &list2, 2);
insert( &list2, 3);
insert( &list2, 1);
largest_2nd_largest(list2);
//Add node in third linked list
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);
//Add node in fourth linked list
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
//Node of LinkedList
class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
public class MyLinkedList
{
public Node head;
public Node tail;
//Class constructor
public MyLinkedList()
{
this.head = null;
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
if (this.head == null)
{
//When linked list empty add first node
this.head = node;
this.tail = node;
}
else
{
//Add new node at end of linked list
this.tail.next = node;
this.tail = node;
}
}
//Display linked list element
public void display()
{
if (this.head == null)
{
System.out.print("\nEmpty linked list\n");
return;
}
Node temp = this.head;
System.out.print("\n Linked List : ");
//iterating linked list elements
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()
{
if (this.head == null)
{
System.out.print("\nEmpty linked list\n");
return;
}
//Display linked list
this.display();
//Define some auxiliary variables
Node big = null;
Node second_big = null;
Node temp = head;
//iterating linked list elements
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)
{
MyLinkedList list1 = new MyLinkedList();
MyLinkedList list2 = new MyLinkedList();
MyLinkedList list3 = new MyLinkedList();
MyLinkedList list4 = new MyLinkedList();
//Add node in first linked list
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();
//Add node in second linked list
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
list2.largest_2nd_largest();
//Add node in third linked list
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();
//Add node in fourth linked list
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
//Node of LinkedList
class Node
{
public:
int data;
Node * next;
Node(int data)
{
//Set node value
this->data = data;
this->next = NULL;
}
};
class MyLinkedList
{
public: Node * head;
Node * tail;
//Class constructor
MyLinkedList()
{
this->head = NULL;
this->tail = NULL;
}
//insert node at last of linke list
void insert(int data)
{
//Create a node
Node * node = new Node(data);
if (this->head == NULL)
{
//When linked list empty add first node
this->head = node;
this->tail = node;
}
else
{
//Add new node at end of linked list
this->tail->next = node;
this->tail = node;
}
}
//Display linked list element
void display()
{
if (this->head == NULL)
{
cout << "\nEmpty linked list\n";
return;
}
Node * temp = this->head;
cout << "\n Linked List : ";
//iterating linked list elements
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()
{
if (this->head == NULL)
{
cout << "\nEmpty linked list\n";
return;
}
//Display linked list
this->display();
//Define some auxiliary variables
Node * big = NULL;
Node * second_big = NULL;
Node * temp = this->head;
//iterating linked list elements
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()
{
MyLinkedList list1 = MyLinkedList();
MyLinkedList list2 = MyLinkedList();
MyLinkedList list3 = MyLinkedList();
MyLinkedList list4 = MyLinkedList();
//Add node in first linked list
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();
//Add node in second linked list
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
list2.largest_2nd_largest();
//Add node in third linked list
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();
//Add node in fourth linked list
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
//Node of LinkedList
public class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
public class MyLinkedList
{
public Node head;
public Node tail;
//Class constructor
public MyLinkedList()
{
this.head = null;
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
if (this.head == null)
{
//When linked list empty add first node
this.head = node;
this.tail = node;
}
else
{
//Add new node at end of linked list
this.tail.next = node;
this.tail = node;
}
}
//Display linked list element
public void display()
{
if (this.head == null)
{
Console.Write("\nEmpty linked list\n");
return;
}
Node temp = this.head;
Console.Write("\n Linked List : ");
//iterating linked list elements
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()
{
if (this.head == null)
{
Console.Write("\nEmpty linked list\n");
return;
}
//Display linked list
this.display();
//Define some auxiliary variables
Node big = null;
Node second_big = null;
Node temp = head;
//iterating linked list elements
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)
{
MyLinkedList list1 = new MyLinkedList();
MyLinkedList list2 = new MyLinkedList();
MyLinkedList list3 = new MyLinkedList();
MyLinkedList list4 = new MyLinkedList();
//Add node in first linked list
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();
//Add node in second linked list
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
list2.largest_2nd_largest();
//Add node in third linked list
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();
//Add node in fourth linked list
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
//Node of LinkedList
class Node
{
public $data;
public $next;
function __construct($data)
{
//Set node value
$this->data = $data;
$this->next = null;
}
}
class MyLinkedList
{
public $head;
public $tail;
//Class constructor
function __construct()
{
$this->head = null;
$this->tail = null;
}
//insert node at last of linke list
public function insert($data)
{
//Create a node
$node = new Node($data);
if ($this->head == null)
{
//When linked list empty add first node
$this->head = $node;
$this->tail = $node;
}
else
{
//Add new node at end of linked list
$this->tail->next = $node;
$this->tail = $node;
}
}
//Display linked list element
public function display()
{
if ($this->head == null)
{
echo "\nEmpty linked list\n";
return;
}
$temp = $this->head;
echo "\n Linked List : ";
//iterating linked list elements
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()
{
if ($this->head == null)
{
echo "\nEmpty linked list\n";
return;
}
//Display linked list
$this->display();
//Define some auxiliary variables
$big = null;
$second_big = null;
$temp = $this->head;
//iterating linked list elements
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 = new MyLinkedList();
$list2 = new MyLinkedList();
$list3 = new MyLinkedList();
$list4 = new MyLinkedList();
//Add node in first linked list
$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();
//Add node in second linked list
$list2->insert(1);
$list2->insert(1);
$list2->insert(4);
$list2->insert(2);
$list2->insert(3);
$list2->insert(1);
$list2->largest_2nd_largest();
//Add node in third linked list
$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();
//Add node in fourth linked list
$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
//Node of LinkedList
class Node
{
constructor(data)
{
//Set node value
this.data = data;
this.next = null;
}
}
class MyLinkedList
{
//Class constructor
constructor()
{
this.head = null;
this.tail = null;
}
//insert node at last of linke list
insert(data)
{
//Create a node
var node = new Node(data);
if (this.head == null)
{
//When linked list empty add first node
this.head = node;
this.tail = node;
}
else
{
//Add new node at end of linked list
this.tail.next = node;
this.tail = node;
}
}
//Display linked list element
display()
{
if (this.head == null)
{
process.stdout.write("\nEmpty linked list\n");
return;
}
var temp = this.head;
process.stdout.write("\n Linked List : ");
//iterating linked list elements
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()
{
if (this.head == null)
{
process.stdout.write("\nEmpty linked list\n");
return;
}
//Display linked list
this.display();
//Define some auxiliary variables
var big = null;
var second_big = null;
var temp = this.head;
//iterating linked list elements
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()
{
var list1 = new MyLinkedList();
var list2 = new MyLinkedList();
var list3 = new MyLinkedList();
var list4 = new MyLinkedList();
//Add node in first linked list
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();
//Add node in second linked list
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
list2.largest_2nd_largest();
//Add node in third linked list
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();
//Add node in fourth linked list
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
# Node of LinkedList
class Node :
def __init__(self, data) :
# Set node value
self.data = data
self.next = None
class MyLinkedList :
# Class constructor
def __init__(self) :
self.head = None
self.tail = None
# insert node at last of linke list
def insert(self, data) :
# Create a node
node = Node(data)
if (self.head == None) :
# When linked list empty add first node
self.head = node
self.tail = node
else :
# Add new node at end of linked list
self.tail.next = node
self.tail = node
# Display linked list element
def display(self) :
if (self.head == None) :
print("\nEmpty linked list\n", end = "")
return
temp = self.head
print("\n Linked List : ", end = "")
# iterating linked list elements
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) :
if (self.head == None) :
print("\nEmpty linked list\n", end = "")
return
# Display linked list
self.display()
# Define some auxiliary variables
big = None
second_big = None
temp = self.head
# iterating linked list elements
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 = MyLinkedList()
list2 = MyLinkedList()
list3 = MyLinkedList()
list4 = MyLinkedList()
# Add node in first linked list
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()
# Add node in second linked list
list2.insert(1)
list2.insert(1)
list2.insert(4)
list2.insert(2)
list2.insert(3)
list2.insert(1)
list2.largest_2nd_largest()
# Add node in third linked list
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()
# Add node in fourth linked list
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
# Node of LinkedList
class Node
# Define the accessor and reader of class Node
attr_reader :data, :next
attr_accessor :data, :next
def initialize(data)
# Set node value
self.data = data
self.next = nil
end
end
class MyLinkedList
# Define the accessor and reader of class MyLinkedList
attr_reader :head, :tail
attr_accessor :head, :tail
# Class constructor
def initialize()
self.head = nil
self.tail = nil
end
# insert node at last of linke list
def insert(data)
# Create a node
node = Node.new(data)
if (self.head == nil)
# When linked list empty add first node
self.head = node
self.tail = node
else
# Add new node at end of linked list
self.tail.next = node
self.tail = node
end
end
# Display linked list element
def display()
if (self.head == nil)
print("\nEmpty linked list\n")
return
end
temp = self.head
print("\n Linked List : ")
# iterating linked list elements
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()
if (self.head == nil)
print("\nEmpty linked list\n")
return
end
# Display linked list
self.display()
# Define some auxiliary variables
big = nil
second_big = nil
temp = @head
# iterating linked list elements
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 = MyLinkedList.new()
list2 = MyLinkedList.new()
list3 = MyLinkedList.new()
list4 = MyLinkedList.new()
# Add node in first linked list
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()
# Add node in second linked list
list2.insert(1)
list2.insert(1)
list2.insert(4)
list2.insert(2)
list2.insert(3)
list2.insert(1)
list2.largest_2nd_largest()
# Add node in third linked list
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()
# Add node in fourth linked list
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
//Node of LinkedList
class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
class MyLinkedList(var head: Node,
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);
if (this.head == null)
{
//When linked list empty add first node
this.head = node;
this.tail = node;
}
else
{
//Add new node at end of linked list
this.tail.next = node;
this.tail = node;
}
}
//Display linked list element
def display(): Unit = {
if (this.head == null)
{
print("\nEmpty linked list\n");
return;
}
var temp: Node = this.head;
print("\n Linked List : ");
//iterating linked list elements
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 = {
if (this.head == null)
{
print("\nEmpty linked list\n");
return;
}
//Display linked list
this.display();
//Define some auxiliary variables
var big: Node = null;
var second_big: Node = null;
var temp: Node = head;
//iterating linked list elements
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 = {
var list1: MyLinkedList = new MyLinkedList();
var list2: MyLinkedList = new MyLinkedList();
var list3: MyLinkedList = new MyLinkedList();
var list4: MyLinkedList = new MyLinkedList();
//Add node in first linked list
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();
//Add node in second linked list
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
list2.largest_2nd_largest();
//Add node in third linked list
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();
//Add node in fourth linked list
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
//Node of LinkedList
class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int)
{
//Set node value
self.data = data;
self.next = nil;
}
}
class MyLinkedList
{
var head: Node? ;
var tail: Node? ;
//Class constructor
init()
{
self.head = nil;
self.tail = nil;
}
//insert node at last of linke list
func insert(_ data: Int)
{
//Create a node
let node: Node? = Node(data);
if (self.head == nil)
{
//When linked list empty add first node
self.head = node;
self.tail = node;
}
else
{
//Add new node at end of linked list
self.tail!.next = node;
self.tail = node;
}
}
//Display linked list element
func display()
{
if (self.head == nil)
{
print("\nEmpty linked list\n", terminator: "");
return;
}
var temp: Node? = self.head;
print("\n Linked List : ", terminator: "");
//iterating linked list elements
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()
{
if (self.head == nil)
{
print("\nEmpty linked list\n", terminator: "");
return;
}
//Display linked list
self.display();
//Define some auxiliary variables
var big: Node? = nil;
var second_big: Node? = nil;
var temp: Node? = self.head;
//iterating linked list elements
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()
{
let list1: MyLinkedList = MyLinkedList();
let list2: MyLinkedList = MyLinkedList();
let list3: MyLinkedList = MyLinkedList();
let list4: MyLinkedList = MyLinkedList();
//Add node in first linked list
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();
//Add node in second linked list
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
list2.largest_2nd_largest();
//Add node in third linked list
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();
//Add node in fourth linked list
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
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.
New Comment