Select a random node from a singly linked list
Here given code implementation process.
// C program for
// Select a random node from a singly linked list
// using 1/n probability
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Linked List LinkNode
struct LinkNode
{
int data;
struct LinkNode *next;
};
// Singly linked list
struct SingleLL
{
struct LinkNode *head;
struct LinkNode *tail;
};
// Returns the new linked list
struct SingleLL *newLinkedList()
{
// Create memory of head and tail Nodes
struct SingleLL *sll =
(struct SingleLL *) malloc(sizeof(struct SingleLL));
if (sll == NULL)
{
printf("Memory overflow\n");
}
else
{
sll->head = NULL;
}
return sll;
}
// Handles the request of adding new node in linked list
void addNode(struct SingleLL *sll, int data)
{
// Create dynamic node
struct LinkNode *node =
(struct LinkNode *) malloc(sizeof(struct LinkNode));
if (node == NULL)
{
printf("Memory overflow to Create LinkNode\n");
return;
}
else
{
// Set initial node value
node->data = data;
node->next = NULL;
}
if (sll->head == NULL)
{
sll->head = node;
}
else
{
sll->tail->next = node;
}
sll->tail = node;
}
// Display linked list element
void display(struct LinkNode *node)
{
if (node == NULL)
{
return;
}
struct LinkNode *temp = node;
// iterating linked list elements
while (temp != NULL)
{
printf(" %d →", temp->data);
// Visit to next node
temp = temp->next;
}
printf(" NULL\n");
}
void randomNode(struct LinkNode *node)
{
struct LinkNode * temp = node;
struct LinkNode * result = node;
int n = 2;
while(temp != NULL)
{
if(rand() % n == 0)
{
// Get new resultant node
result = temp;
}
// visit to next node
temp = temp -> next;
// increase the value of n by one
n++;
}
if(result==NULL)
{
printf("\n None \n");
}
else
{
printf(" Result : %d \n", result->data);
}
}
int main(int argc, char const *argv[])
{
srand(time(NULL));
struct SingleLL *sll1 = newLinkedList();
struct SingleLL *sll2 = newLinkedList();
// First linked list
// 5 → 2 → -2 → 1 → 3 → 6 → NULL
addNode(sll1, 5);
addNode(sll1, 2);
addNode(sll1, -2);
addNode(sll1, 1);
addNode(sll1, 3);
addNode(sll1, 6);
// Second linked list
// -1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
addNode(sll2, -1);
addNode(sll2, 7);
addNode(sll2, 5);
addNode(sll2, 8);
addNode(sll2, 9);
addNode(sll2, -9);
addNode(sll2, 10);
// Test in first LL
display(sll1->head);
randomNode(sll1->head);
randomNode(sll1->head);
randomNode(sll1->head);
// Test in second LL
display(sll2->head);
randomNode(sll2->head);
randomNode(sll2->head);
return 0;
}
Output
5 → 2 → -2 → 1 → 3 → 6 → NULL
Result : 5
Result : 1
Result : 2
-1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
Result : 9
Result : -1
import java.util.Random;
/*
Java Program for
Find a peak element in matrix
*/
// Linked list node
class LinkNode
{
public int data;
public LinkNode next;
public LinkNode(int data)
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public LinkNode head;
public LinkNode tail;
public SingleLL()
{
this.head = null;
this.tail = null;
}
// Add new Node at end of linked list
public void addNode(int data)
{
LinkNode node = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
public void display()
{
if (this.head == null)
{
return;
}
LinkNode temp = this.head;
// iterating linked list elements
while (temp != null)
{
System.out.print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
System.out.print(" NULL\n");
}
public int randomValue()
{
Random rand = new Random();
int x = rand.nextInt();
if (x < 0)
{
return -x;
}
return x;
}
public void randomNode()
{
LinkNode temp = this.head;
LinkNode result = this.head;
int n = 2;
while (temp != null)
{
if (randomValue() % n == 0)
{
// Get new resultant node
result = temp;
}
// visit to next node
temp = temp.next;
// increase the value of n by one
n++;
}
if (result == null)
{
System.out.print("\n None \n");
}
else
{
System.out.println(" Result : " + result.data);
}
}
public static void main(String[] args)
{
SingleLL sll1 = new SingleLL();
SingleLL sll2 = new SingleLL();
// First linked list
// 5 → 2 → -2 → 1 → 3 → 6 → NULL
sll1.addNode(5);
sll1.addNode(2);
sll1.addNode(-2);
sll1.addNode(1);
sll1.addNode(3);
sll1.addNode(6);
// Second linked list
// -1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
sll2.addNode(-1);
sll2.addNode(7);
sll2.addNode(5);
sll2.addNode(8);
sll2.addNode(9);
sll2.addNode(-9);
sll2.addNode(10);
// Test in first LL
sll1.display();
sll1.randomNode();
sll1.randomNode();
sll1.randomNode();
// Test in second LL
sll2.display();
sll2.randomNode();
sll2.randomNode();
}
}
Output
5 → 2 → -2 → 1 → 3 → 6 → NULL
Result : 6
Result : 3
Result : 5
-1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
Result : 7
Result : -9
// Include header file
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
/*
C++ Program for
Find a peak element in matrix
*/
// Linked list node
class LinkNode
{
public: int data;
LinkNode *next;
LinkNode(int data)
{
this->data = data;
this->next = NULL;
}
};
class SingleLL
{
public: LinkNode *head;
LinkNode *tail;
SingleLL()
{
this->head = NULL;
this->tail = NULL;
}
// Add new Node at end of linked list
void addNode(int data)
{
LinkNode *node = new LinkNode(data);
if (this->head == NULL)
{
this->head = node;
}
else
{
// Append the node at last position
this->tail->next = node;
}
this->tail = node;
}
// Display linked list element
void display()
{
if (this->head == NULL)
{
return;
}
LinkNode *temp = this->head;
// iterating linked list elements
while (temp != NULL)
{
cout << " " << temp->data << " →";
// Visit to next node
temp = temp->next;
}
cout << " NULL\n";
}
int randomValue()
{
int x = rand();
if (x < 0)
{
return -x;
}
return x;
}
void randomNode()
{
LinkNode *temp = this->head;
LinkNode *result = this->head;
int n = 2;
while (temp != NULL)
{
if (this->randomValue() % n == 0)
{
// Get new resultant node
result = temp;
}
// visit to next node
temp = temp->next;
// increase the value of n by one
n++;
}
if (result == NULL)
{
cout << "\n None \n";
}
else
{
cout << " Result : " << result->data << endl;
}
}
};
int main()
{
srand(time(NULL));
SingleLL *sll1 = new SingleLL();
SingleLL *sll2 = new SingleLL();
// First linked list
// 5 → 2 → -2 → 1 → 3 → 6 → NULL
sll1->addNode(5);
sll1->addNode(2);
sll1->addNode(-2);
sll1->addNode(1);
sll1->addNode(3);
sll1->addNode(6);
// Second linked list
// -1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
sll2->addNode(-1);
sll2->addNode(7);
sll2->addNode(5);
sll2->addNode(8);
sll2->addNode(9);
sll2->addNode(-9);
sll2->addNode(10);
// Test in first LL
sll1->display();
sll1->randomNode();
sll1->randomNode();
sll1->randomNode();
// Test in second LL
sll2->display();
sll2->randomNode();
sll2->randomNode();
return 0;
}
Output
5 → 2 → -2 → 1 → 3 → 6 → NULL
Result : 6
Result : -2
Result : 5
-1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
Result : 8
Result : -9
// Include namespace system
using System;
/*
Csharp Program for
Find a peak element in matrix
*/
// Linked list node
public class LinkNode
{
public int data;
public LinkNode next;
public LinkNode(int data)
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public LinkNode head;
public LinkNode tail;
public SingleLL()
{
this.head = null;
this.tail = null;
}
// Add new Node at end of linked list
public void addNode(int data)
{
LinkNode node = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
public void display()
{
if (this.head == null)
{
return;
}
LinkNode temp = this.head;
// iterating linked list elements
while (temp != null)
{
Console.Write(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
Console.Write(" NULL\n");
}
public int randomValue()
{
Random rand = new Random();
int x = rand.Next();
if (x < 0)
{
return -x;
}
return x;
}
public void randomNode()
{
LinkNode temp = this.head;
LinkNode result = this.head;
int n = 2;
while (temp != null)
{
if (this.randomValue() % n == 0)
{
// Get new resultant node
result = temp;
}
// visit to next node
temp = temp.next;
// increase the value of n by one
n++;
}
if (result == null)
{
Console.Write("\n None \n");
}
else
{
Console.WriteLine(" Result : " + result.data);
}
}
public static void Main(String[] args)
{
SingleLL sll1 = new SingleLL();
SingleLL sll2 = new SingleLL();
// First linked list
// 5 → 2 → -2 → 1 → 3 → 6 → NULL
sll1.addNode(5);
sll1.addNode(2);
sll1.addNode(-2);
sll1.addNode(1);
sll1.addNode(3);
sll1.addNode(6);
// Second linked list
// -1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
sll2.addNode(-1);
sll2.addNode(7);
sll2.addNode(5);
sll2.addNode(8);
sll2.addNode(9);
sll2.addNode(-9);
sll2.addNode(10);
// Test in first LL
sll1.display();
sll1.randomNode();
sll1.randomNode();
sll1.randomNode();
// Test in second LL
sll2.display();
sll2.randomNode();
sll2.randomNode();
}
}
Output
5 → 2 → -2 → 1 → 3 → 6 → NULL
Result : 3
Result : 1
Result : 3
-1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
Result : 9
Result : -1
package main
import (
"fmt"
"math/rand"
"math"
"time"
)
/*
Go Program for
Find a peak element in matrix
*/
// Linked list node
type LinkNode struct {
data int
next * LinkNode
}
func getLinkNode(data int) * LinkNode {
var me *LinkNode = &LinkNode {}
me.data = data
me.next = nil
return me
}
type SingleLL struct {
head * LinkNode
tail * LinkNode
}
func getSingleLL() * SingleLL {
var me *SingleLL = &SingleLL {}
me.head = nil
me.tail = nil
return me
}
// Add new Node at end of linked list
func(this *SingleLL) addNode(data int) {
var node * LinkNode = getLinkNode(data)
if this.head == nil {
this.head = node
} else {
// Append the node at last position
this.tail.next = node
}
this.tail = node
}
// Display linked list element
func(this SingleLL) display() {
if this.head == nil {
return
}
var temp * LinkNode = this.head
// iterating linked list elements
for (temp != nil) {
fmt.Print(" ", temp.data, " →")
// Visit to next node
temp = temp.next
}
fmt.Print(" NULL\n")
}
func(this SingleLL) randomNode() {
var temp * LinkNode = this.head
var result * LinkNode = this.head
var n int = 2
rand.Seed(time.Now().UnixNano())
for (temp != nil) {
if rand.Intn(math.MaxInt64-1) % n == 0 {
// Get new resultant node
result = temp
}
// visit to next node
temp = temp.next
// increase the value of n by one
n++
}
if result == nil {
fmt.Print("\n None \n")
} else {
fmt.Println(" Result : ", result.data)
}
}
func main() {
var sll1 * SingleLL = getSingleLL()
var sll2 * SingleLL = getSingleLL()
// First linked list
// 5 → 2 → -2 → 1 → 3 → 6 → NULL
sll1.addNode(5)
sll1.addNode(2)
sll1.addNode(-2)
sll1.addNode(1)
sll1.addNode(3)
sll1.addNode(6)
// Second linked list
// -1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
sll2.addNode(-1)
sll2.addNode(7)
sll2.addNode(5)
sll2.addNode(8)
sll2.addNode(9)
sll2.addNode(-9)
sll2.addNode(10)
// Test in first LL
sll1.display()
sll1.randomNode()
sll1.randomNode()
sll1.randomNode()
// Test in second LL
sll2.display()
sll2.randomNode()
sll2.randomNode()
}
Output
5 → 2 → -2 → 1 → 3 → 6 → NULL
Result : 3
Result : 6
Result : 5
-1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
Result : -1
Result : 10
<?php
/*
Php Program for
Find a peak element in matrix
*/
// Linked list node
class LinkNode
{
public $data;
public $next;
public function __construct($data)
{
$this->data = $data;
$this->next = NULL;
}
}
class SingleLL
{
public $head;
public $tail;
public function __construct()
{
$this->head = NULL;
$this->tail = NULL;
}
// Add new Node at end of linked list
public function addNode($data)
{
$node = new LinkNode($data);
if ($this->head == NULL)
{
$this->head = $node;
}
else
{
// Append the node at last position
$this->tail->next = $node;
}
$this->tail = $node;
}
// Display linked list element
public function display()
{
if ($this->head == NULL)
{
return;
}
$temp = $this->head;
// iterating linked list elements
while ($temp != NULL)
{
echo(" ".$temp->data." →");
// Visit to next node
$temp = $temp->next;
}
echo(" NULL\n");
}
public function randomValue()
{
$x = rand();
if ($x < 0)
{
return -$x;
}
return $x;
}
public function randomNode()
{
$temp = $this->head;
$result = $this->head;
$n = 2;
while ($temp != NULL)
{
if ($this->randomValue() % $n == 0)
{
// Get new resultant node
$result = $temp;
}
// visit to next node
$temp = $temp->next;
// increase the value of n by one
$n++;
}
if ($result == NULL)
{
echo("\n None \n");
}
else
{
echo(" Result : ".$result->data."\n");
}
}
}
function main()
{
$sll1 = new SingleLL();
$sll2 = new SingleLL();
// First linked list
// 5 → 2 → -2 → 1 → 3 → 6 → NULL
$sll1->addNode(5);
$sll1->addNode(2);
$sll1->addNode(-2);
$sll1->addNode(1);
$sll1->addNode(3);
$sll1->addNode(6);
// Second linked list
// -1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
$sll2->addNode(-1);
$sll2->addNode(7);
$sll2->addNode(5);
$sll2->addNode(8);
$sll2->addNode(9);
$sll2->addNode(-9);
$sll2->addNode(10);
// Test in first LL
$sll1->display();
$sll1->randomNode();
$sll1->randomNode();
$sll1->randomNode();
// Test in second LL
$sll2->display();
$sll2->randomNode();
$sll2->randomNode();
}
main();
Output
5 → 2 → -2 → 1 → 3 → 6 → NULL
Result : 5
Result : 3
Result : -2
-1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
Result : 8
Result : 5
/*
Node JS Program for
Find a peak element in matrix
*/
// Linked list node
class LinkNode
{
constructor(data)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
constructor()
{
this.head = null;
this.tail = null;
}
// Add new Node at end of linked list
addNode(data)
{
var node = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
display()
{
if (this.head == null)
{
return;
}
var temp = this.head;
// iterating linked list elements
while (temp != null)
{
process.stdout.write(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
process.stdout.write(" NULL\n");
}
randomValue()
{
var x = Math.floor(Math.random() * Math.floor(100000));
if (x < 0)
{
return -x;
}
return x;
}
randomNode()
{
var temp = this.head;
var result = this.head;
var n = 2;
while (temp != null)
{
if (this.randomValue() % n == 0)
{
// Get new resultant node
result = temp;
}
// visit to next node
temp = temp.next;
// increase the value of n by one
n++;
}
if (result == null)
{
process.stdout.write("\n None \n");
}
else
{
console.log(" Result : " + result.data);
}
}
}
function main()
{
var sll1 = new SingleLL();
var sll2 = new SingleLL();
// First linked list
// 5 → 2 → -2 → 1 → 3 → 6 → NULL
sll1.addNode(5);
sll1.addNode(2);
sll1.addNode(-2);
sll1.addNode(1);
sll1.addNode(3);
sll1.addNode(6);
// Second linked list
// -1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
sll2.addNode(-1);
sll2.addNode(7);
sll2.addNode(5);
sll2.addNode(8);
sll2.addNode(9);
sll2.addNode(-9);
sll2.addNode(10);
// Test in first LL
sll1.display();
sll1.randomNode();
sll1.randomNode();
sll1.randomNode();
// Test in second LL
sll2.display();
sll2.randomNode();
sll2.randomNode();
}
main();
Output
5 → 2 → -2 → 1 → 3 → 6 → NULL
Result : 5
Result : 1
Result : 3
-1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
Result : 9
Result : -1
# Python 3 Program for
# Find a peak element in matrix
import random
import sys
# Linked list node
class LinkNode :
def __init__(self, data) :
self.data = data
self.next = None
class SingleLL :
def __init__(self) :
self.head = None
self.tail = None
# Add new Node at end of linked list
def addNode(self, data) :
node = LinkNode(data)
if (self.head == None) :
self.head = node
else :
# Append the node at last position
self.tail.next = node
self.tail = node
# Display linked list element
def display(self) :
if (self.head == None) :
return
temp = self.head
# iterating linked list elements
while (temp != None) :
print("", temp.data ," →", end = "")
# Visit to next node
temp = temp.next
print(" NULL")
def randomValue(self) :
x = random.randint(0,sys.maxsize-2)
if (x < 0) :
return -x
return x
def randomNode(self) :
temp = self.head
result = self.head
n = 2
while (temp != None) :
if (self.randomValue() % n == 0) :
# Get new resultant node
result = temp
# visit to next node
temp = temp.next
# increase the value of n by one
n += 1
if (result == None) :
print("\n None ")
else :
print(" Result : ", result.data)
def main() :
sll1 = SingleLL()
sll2 = SingleLL()
# First linked list
# 5 → 2 → -2 → 1 → 3 → 6 → NULL
sll1.addNode(5)
sll1.addNode(2)
sll1.addNode(-2)
sll1.addNode(1)
sll1.addNode(3)
sll1.addNode(6)
# Second linked list
# -1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
sll2.addNode(-1)
sll2.addNode(7)
sll2.addNode(5)
sll2.addNode(8)
sll2.addNode(9)
sll2.addNode(-9)
sll2.addNode(10)
# Test in first LL
sll1.display()
sll1.randomNode()
sll1.randomNode()
sll1.randomNode()
# Test in second LL
sll2.display()
sll2.randomNode()
sll2.randomNode()
if __name__ == "__main__": main()
Output
5 → 2 → -2 → 1 → 3 → 6 → NULL
Result : 6
Result : 2
Result : -2
-1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
Result : 5
Result : 5
# Ruby Program for
# Find a peak element in matrix
# Linked list node
class LinkNode
# Define the accessor and reader of class LinkNode
attr_reader :data, :next
attr_accessor :data, :next
def initialize(data)
self.data = data
self.next = nil
end
end
class SingleLL
# Define the accessor and reader of class SingleLL
attr_reader :head, :tail
attr_accessor :head, :tail
def initialize()
self.head = nil
self.tail = nil
end
# Add new Node at end of linked list
def addNode(data)
node = LinkNode.new(data)
if (self.head == nil)
self.head = node
else
# Append the node at last position
self.tail.next = node
end
self.tail = node
end
# Display linked list element
def display()
if (self.head == nil)
return
end
temp = self.head
# iterating linked list elements
while (temp != nil)
print(" ", temp.data ," →")
# Visit to next node
temp = temp.next
end
print(" NULL\n")
end
def randomValue()
r = Random.new
max_int = (2**(0.size * 8 -2) -1)
x = r.rand(0...max_int)
if (x < 0)
return -x
end
return x
end
def randomNode()
temp = self.head
result = self.head
n = 2
while (temp != nil)
if (self.randomValue() % n == 0)
# Get new resultant node
result = temp
end
# visit to next node
temp = temp.next
# increase the value of n by one
n += 1
end
if (result == nil)
print("\n None \n")
else
print(" Result : ", result.data, "\n")
end
end
end
def main()
sll1 = SingleLL.new()
sll2 = SingleLL.new()
# First linked list
# 5 → 2 → -2 → 1 → 3 → 6 → NULL
sll1.addNode(5)
sll1.addNode(2)
sll1.addNode(-2)
sll1.addNode(1)
sll1.addNode(3)
sll1.addNode(6)
# Second linked list
# -1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
sll2.addNode(-1)
sll2.addNode(7)
sll2.addNode(5)
sll2.addNode(8)
sll2.addNode(9)
sll2.addNode(-9)
sll2.addNode(10)
# Test in first LL
sll1.display()
sll1.randomNode()
sll1.randomNode()
sll1.randomNode()
# Test in second LL
sll2.display()
sll2.randomNode()
sll2.randomNode()
end
main()
Output
5 → 2 → -2 → 1 → 3 → 6 → NULL
Result : -2
Result : 3
Result : 6
-1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
Result : -1
Result : 7
/*
Scala Program for
Find a peak element in matrix
*/
// Linked list node
class LinkNode(var data: Int,
var next: LinkNode)
{
def this(data: Int)
{
this(data, null)
}
}
class SingleLL(var head: LinkNode,
var tail: LinkNode)
{
def this()
{
this(null, null)
}
// Add new Node at end of linked list
def addNode(data: Int): Unit = {
var node: LinkNode = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
def display(): Unit = {
if (this.head == null)
{
return;
}
var temp: LinkNode = this.head;
// iterating linked list elements
while (temp != null)
{
print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
print(" NULL\n");
}
def randomValue(): Int = {
var rand = new scala.util.Random;
var x: Int = rand.nextInt();
if (x < 0)
{
return -x;
}
return x;
}
def randomNode(): Unit = {
var temp: LinkNode = this.head;
var result: LinkNode = this.head;
var n: Int = 2;
while (temp != null)
{
if (randomValue() % n == 0)
{
// Get new resultant node
result = temp;
}
// visit to next node
temp = temp.next;
// increase the value of n by one
n += 1;
}
if (result == null)
{
print("\n None \n");
}
else
{
println(" Result : " + result.data);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var sll1: SingleLL = new SingleLL();
var sll2: SingleLL = new SingleLL();
// First linked list
// 5 → 2 → -2 → 1 → 3 → 6 → NULL
sll1.addNode(5);
sll1.addNode(2);
sll1.addNode(-2);
sll1.addNode(1);
sll1.addNode(3);
sll1.addNode(6);
// Second linked list
// -1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
sll2.addNode(-1);
sll2.addNode(7);
sll2.addNode(5);
sll2.addNode(8);
sll2.addNode(9);
sll2.addNode(-9);
sll2.addNode(10);
// Test in first LL
sll1.display();
sll1.randomNode();
sll1.randomNode();
sll1.randomNode();
// Test in second LL
sll2.display();
sll2.randomNode();
sll2.randomNode();
}
}
Output
5 → 2 → -2 → 1 → 3 → 6 → NULL
Result : -2
Result : 3
Result : 5
-1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
Result : 9
Result : 10
/*
Swift 4 Program for
Find a peak element in matrix
*/
import Foundation
#if os(Linux)
srandom(UInt32(time(nil)))
#endif
// Linked list node
class LinkNode
{
var data: Int;
var next: LinkNode? ;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
}
class SingleLL
{
var head: LinkNode? ;
var tail: LinkNode? ;
init()
{
self.head = nil;
self.tail = nil;
}
// Add new Node at end of linked list
func addNode(_ data: Int)
{
let node: LinkNode = LinkNode(data);
if (self.head == nil)
{
self.head = node;
}
else
{
// Append the node at last position
self.tail!.next = node;
}
self.tail = node;
}
// Display linked list element
func display()
{
if (self.head == nil)
{
return;
}
var temp: LinkNode? = self.head;
// iterating linked list elements
while (temp != nil)
{
print(" ", temp!.data ," →", terminator: "");
// Visit to next node
temp = temp!.next;
}
print(" NULL");
}
func randomValue() -> Int
{
var x: Int = 0;
let max_value = UInt8.max-1 ;
//Get new rand number
#if os(Linux)
x = Int(random())
#else
x = Int(arc4random_uniform(UInt32(max_value)))
#endif
if(x < 0)
{
return -x;
}
return x;
}
func randomNode()
{
var temp: LinkNode? = self.head;
var result: LinkNode? = self.head;
var n: Int = 2;
while (temp != nil)
{
if (self.randomValue() % n == 0)
{
// Get new resultant node
result = temp;
}
// visit to next node
temp = temp!.next;
// increase the value of n by one
n += 1;
}
if (result == nil)
{
print("\n None ");
}
else
{
print(" Result : ", result!.data);
}
}
}
func main()
{
let sll1: SingleLL = SingleLL();
let sll2: SingleLL = SingleLL();
// First linked list
// 5 → 2 → -2 → 1 → 3 → 6 → NULL
sll1.addNode(5);
sll1.addNode(2);
sll1.addNode(-2);
sll1.addNode(1);
sll1.addNode(3);
sll1.addNode(6);
// Second linked list
// -1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
sll2.addNode(-1);
sll2.addNode(7);
sll2.addNode(5);
sll2.addNode(8);
sll2.addNode(9);
sll2.addNode(-9);
sll2.addNode(10);
// Test in first LL
sll1.display();
sll1.randomNode();
sll1.randomNode();
sll1.randomNode();
// Test in second LL
sll2.display();
sll2.randomNode();
sll2.randomNode();
}
main();
Output
5 → 2 → -2 → 1 → 3 → 6 → NULL
Result : 3
Result : -2
Result : 2
-1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
Result : 7
Result : 9
/*
Kotlin Program for
Find a peak element in matrix
*/
// Linked list node
class LinkNode
{
var data: Int;
var next: LinkNode ? ;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
var head: LinkNode ? ;
var tail: LinkNode ? ;
constructor()
{
this.head = null;
this.tail = null;
}
// Add new Node at end of linked list
fun addNode(data: Int): Unit
{
val node: LinkNode = LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail?.next = node;
}
this.tail = node;
}
// Display linked list element
fun display(): Unit
{
if (this.head == null)
{
return;
}
var temp: LinkNode ? = this.head;
// iterating linked list elements
while (temp != null)
{
print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
print(" NULL\n");
}
fun randomValue(): Int
{
var x: Int = (0..Int.MAX_VALUE).random()
return x;
}
fun randomNode(): Unit
{
var temp: LinkNode ? = this.head;
var result: LinkNode ? = this.head;
var n: Int = 2;
while (temp != null)
{
if (this.randomValue() % n == 0)
{
// Get new resultant node
result = temp;
}
// visit to next node
temp = temp.next;
// increase the value of n by one
n += 1;
}
if (result == null)
{
print("\n None \n");
}
else
{
println(" Result : " + result.data);
}
}
}
fun main(args: Array < String > ): Unit
{
val sll1: SingleLL = SingleLL();
val sll2: SingleLL = SingleLL();
// First linked list
// 5 → 2 → -2 → 1 → 3 → 6 → NULL
sll1.addNode(5);
sll1.addNode(2);
sll1.addNode(-2);
sll1.addNode(1);
sll1.addNode(3);
sll1.addNode(6);
// Second linked list
// -1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
sll2.addNode(-1);
sll2.addNode(7);
sll2.addNode(5);
sll2.addNode(8);
sll2.addNode(9);
sll2.addNode(-9);
sll2.addNode(10);
// Test in first LL
sll1.display();
sll1.randomNode();
sll1.randomNode();
sll1.randomNode();
// Test in second LL
sll2.display();
sll2.randomNode();
sll2.randomNode();
}
Output
5 → 2 → -2 → 1 → 3 → 6 → NULL
Result : 3
Result : 6
Result : 5
-1 → 7 → 5 → 8 → 9 → -9 → 10 → NULL
Result : -1
Result : 5
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