# Print the nodes of linked list at given indexes

Given a collection of sorted index, Our goal is to find and print all nodes on linked list which is exist on given index. For example.

`````` Example 1
Input List : 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
Index List : 2 → 4 → 7 → NULL
Output     : 8   3   10 [value at index]

Example 2
Input List : 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
Index List : 3 → 5 → 6 → 8 → NULL
Output     : 7  8  6  19``````

This problem are based on traversal of linked list.

``````// C Program
// Print the nodes of linked list at given indexes
#include <stdio.h>
#include <stdlib.h> //for malloc function

{
int data;
};
// Singly linked list
struct SingleLL
{
};
// Returns the new linked list
{
// 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->tail = NULL;
}
return sll;
}
// Add new Node at end of linked list
void appendNode(struct SingleLL *sll, struct LinkNode *node)
{
if (sll->head == NULL)
{
}
else
{
// Append the node at last position
sll->tail->next = node;
}
sll->tail = node;
}
// Handles the request of adding new node in linked list
void addNode(struct SingleLL *sll, int data)
{
// Create dynamic node
if (node == NULL)
{
printf("Memory overflow to Create LinkNode\n");
return;
}
else
{
// Set initial node value
node->data = data;
node->next = NULL;
}
appendNode(sll, node);
}
// Display linked list element
void display(struct SingleLL *sll)
{
if (sll->head == NULL)
{
printf("\n Empty linked list\n");
return;
}
// iterating linked list elements
while (temp != NULL)
{
printf(" %d →", temp->data);
// Visit to next node
temp = temp->next;
}
printf(" NULL\n");
}
// Print the node value at given indexes
void printIndexValue(struct SingleLL *sll1, struct SingleLL *sll2)
{
if (sll1->head == NULL || sll2->head == NULL)
{
return;
}
// Display linked list
printf(" Linked List \n");
display(sll2);
// Display index value
printf(" Index  \n");
display(sll1);
printf("\n Index Value \n");
// Get first node of both linked list
// Define counter variable
int counter = 1;
int status = 0;
// iterating linked list elements
while (key != NULL && value != NULL)
{
if (counter == key->data)
{
status = 1;
printf("  %d", value->data);
key = key->next;
}
// Visit to next node
value = value->next;
counter++;
}
if (status == 0)
{
// When no index element not present
printf(" None \n");
}
}
int main()
{
// Define linked list
struct SingleLL *sll1 = newLinkedList();
struct SingleLL *sll2 = newLinkedList();
// Given sorted index
// 3 → 5 → 6 → 8 → NULL
// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
printIndexValue(sll1, sll2);
return 0;
}``````

#### Output

`````` Linked List
4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
Index
3 → 5 → 6 → 8 → NULL

Index Value
7  8  6  19``````
``````/*
Java Program
Print the nodes of linked list at given indexes
*/
// Linked list node
{
public int data;
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public SingleLL()
{
this.tail = null;
}
// Add new Node at end of linked list
public void addNode(int data)
{
if (this.head == null)
{
}
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)
{
System.out.print("\n Empty linked list\n");
return;
}
//iterating linked list elements
while (temp != null)
{
System.out.print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
System.out.print(" NULL\n");
}
// Print the node value at given indexes
public void printIndexValue(SingleLL values)
{
if (this.head == null || values.head == null)
{
return;
}
// Display linked list
System.out.print(" Linked List \n");
values.display();
// Display index value
System.out.print(" Index \n");
this.display();
System.out.print("\n Index Value \n");
// Get first node of both linked list
// Define counter variable
int counter = 1;
boolean status = false;
// iterating linked list elements
while (key != null && value != null)
{
if (counter == key.data)
{
status = true;
System.out.print(" " + value.data);
key = key.next;
}
// Visit to next node
value = value.next;
counter++;
}
if (status == false)
{
// When no index element not present
System.out.print(" None \n");
}
}
public static void main(String[] args)
{
SingleLL keys = new SingleLL();
SingleLL values = new SingleLL();
// Given sorted index
// 3 → 5 → 6 → 8 → NULL
// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
// Find key value
keys.printIndexValue(values);
}
}``````

#### Output

`````` Linked List
4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
Index
3 → 5 → 6 → 8 → NULL

Index Value
7 8 6 19``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program
Print the nodes of linked list at given indexes
*/
// Linked list node
{
public: int data;
{
this->data = data;
this->next = NULL;
}
};
class SingleLL
{
SingleLL()
{
this->tail = NULL;
}
// Add new Node at end of linked list
{
if (this->head == NULL)
{
}
else
{
// Append the node at last position
this->tail->next = node;
}
this->tail = node;
}
// Display linked list element
void display()
{
if (this->head == NULL)
{
cout << "\n Empty linked list\n";
return;
}
//iterating linked list elements
while (temp != NULL)
{
cout << " " << temp->data << " →";
// Visit to next node
temp = temp->next;
}
cout << " NULL\n";
}
// Print the node value at given indexes
void printIndexValue(SingleLL values)
{
if (this->head == NULL || values.head == NULL)
{
return;
}
// Display linked list
cout << " Linked List \n";
values.display();
// Display index value
cout << " Index \n";
this->display();
cout << "\n Index Value \n";
// Get first node of both linked list
// Define counter variable
int counter = 1;
bool status = false;
// iterating linked list elements
while (key != NULL && value != NULL)
{
if (counter == key->data)
{
status = true;
cout << " " << value->data;
key = key->next;
}
// Visit to next node
value = value->next;
counter++;
}
if (status == false)
{
// When no index element not present
cout << " None \n";
}
}
};
int main()
{
SingleLL keys = SingleLL();
SingleLL values = SingleLL();
// Given sorted index
// 3 → 5 → 6 → 8 → NULL
// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
// Find key value
keys.printIndexValue(values);
return 0;
}``````

#### Output

`````` Linked List
4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
Index
3 → 5 → 6 → 8 → NULL

Index Value
7 8 6 19``````
``````// Include namespace system
using System;
/*
C# Program
Print the nodes of linked list at given indexes
*/
// Linked list node
{
public int data;
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public SingleLL()
{
this.tail = null;
}
// Add new Node at end of linked list
public void addNode(int data)
{
if (this.head == null)
{
}
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)
{
Console.Write("\n Empty linked list\n");
return;
}
//iterating linked list elements
while (temp != null)
{
Console.Write(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
Console.Write(" NULL\n");
}
// Print the node value at given indexes
public void printIndexValue(SingleLL values)
{
if (this.head == null || values.head == null)
{
return;
}
// Display linked list
Console.Write(" Linked List \n");
values.display();
// Display index value
Console.Write(" Index \n");
this.display();
Console.Write("\n Index Value \n");
// Get first node of both linked list
// Define counter variable
int counter = 1;
Boolean status = false;
// iterating linked list elements
while (key != null && value != null)
{
if (counter == key.data)
{
status = true;
Console.Write(" " + value.data);
key = key.next;
}
// Visit to next node
value = value.next;
counter++;
}
if (status == false)
{
// When no index element not present
Console.Write(" None \n");
}
}
public static void Main(String[] args)
{
SingleLL keys = new SingleLL();
SingleLL values = new SingleLL();
// Given sorted index
// 3 → 5 → 6 → 8 → NULL
// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
// Find key value
keys.printIndexValue(values);
}
}``````

#### Output

`````` Linked List
4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
Index
3 → 5 → 6 → 8 → NULL

Index Value
7 8 6 19``````
``````<?php
/*
Php Program
Print the nodes of linked list at given indexes
*/
// Linked list node
{
public \$data;
public \$next;

function __construct(\$data)
{
\$this->data = \$data;
\$this->next = null;
}
}
class SingleLL
{
public \$tail;

function __construct()
{
\$this->tail = null;
}
// Add new Node at end of linked list
{
\$node = new LinkNode(\$data);
if (\$this->head == null)
{
}
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)
{
echo "\n Empty linked list\n";
return;
}
//iterating linked list elements
while (\$temp != null)
{
echo " ". \$temp->data ." →";
// Visit to next node
\$temp = \$temp->next;
}
echo " NULL\n";
}
// Print the node value at given indexes
public	function printIndexValue(\$values)
{
if (\$this->head == null || \$values->head == null)
{
return;
}
// Display linked list
echo " Linked List \n";
\$values->display();
// Display index value
echo " Index \n";
\$this->display();
echo "\n Index Value \n";
// Get first node of both linked list
// Define counter variable
\$counter = 1;
\$status = false;
// iterating linked list elements
while (\$key != null && \$value != null)
{
if (\$counter == \$key->data)
{
\$status = true;
echo " ". \$value->data;
\$key = \$key->next;
}
// Visit to next node
\$value = \$value->next;
\$counter++;
}
if (\$status == false)
{
// When no index element not present
echo " None \n";
}
}
}

function main()
{
\$keys = new SingleLL();
\$values = new SingleLL();
// Given sorted index
// 3 → 5 → 6 → 8 → NULL
// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
// Find key value
\$keys->printIndexValue(\$values);
}
main();``````

#### Output

`````` Linked List
4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
Index
3 → 5 → 6 → 8 → NULL

Index Value
7 8 6 19``````
``````/*
Node Js Program
Print the nodes of linked list at given indexes
*/
// Linked list node
{
constructor(data)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
constructor()
{
this.tail = null;
}
// Add new Node at end of linked list
{
var node = new LinkNode(data);
if (this.head == null)
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
display()
{
if (this.head == null)
{
process.stdout.write("\n Empty linked list\n");
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");
}
// Print the node value at given indexes
printIndexValue(values)
{
if (this.head == null || values.head == null)
{
return;
}
// Display linked list
process.stdout.write(" Linked List \n");
values.display();
// Display index value
process.stdout.write(" Index \n");
this.display();
process.stdout.write("\n Index Value \n");
// Get first node of both linked list
var key = this.head;
var value = values.head;
// Define counter variable
var counter = 1;
var status = false;
// iterating linked list elements
while (key != null && value != null)
{
if (counter == key.data)
{
status = true;
process.stdout.write(" " + value.data);
key = key.next;
}
// Visit to next node
value = value.next;
counter++;
}
if (status == false)
{
// When no index element not present
process.stdout.write(" None \n");
}
}
}

function main()
{
var keys = new SingleLL();
var values = new SingleLL();
// Given sorted index
// 3 → 5 → 6 → 8 → NULL
// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
// Find key value
keys.printIndexValue(values);
}
main();``````

#### Output

`````` Linked List
4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
Index
3 → 5 → 6 → 8 → NULL

Index Value
7 8 6 19``````
``````#   Python 3 Program
#   Print the nodes of linked list at given indexes

#  Linked list node

def __init__(self, data) :
self.data = data
self.next = None

class SingleLL :

def __init__(self) :
self.tail = None

#  Add new Node at end of linked list
def addNode(self, data) :
if (self.head == None) :
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) :
print("\n Empty linked list")
return

# iterating linked list elements
while (temp != None) :
print("", temp.data ,"→", end = "")
#  Visit to next node
temp = temp.next

print(" NULL")

#  Print the node value at given indexes
def printIndexValue(self, values) :
if (self.head == None or values.head == None) :
return

#  Display linked list
print(" Linked List ")
values.display()
#  Display index value
print(" Index ")
self.display()
print("\n Index Value ")
#  Get first node of both linked list
#  Define counter variable
counter = 1
status = False
#  iterating linked list elements
while (key != None and value != None) :
if (counter == key.data) :
status = True
print(" ", value.data, end = "")
key = key.next

#  Visit to next node
value = value.next
counter += 1

if (status == False) :
#  When no index element not present
print(" None ")

def main() :
keys = SingleLL()
values = SingleLL()
#  Given sorted index
#  3 → 5 → 6 → 8 → NULL
#  4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
#  Find key value
keys.printIndexValue(values)

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

#### Output

`````` Linked List
4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
Index
3 → 5 → 6 → 8 → NULL

Index Value
7  8  6  19``````
``````#   Ruby Program
#   Print the nodes of linked list at given indexes

#  Linked list node
# Define the accessor and reader of class LinkNode
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

def initialize()
self.tail = nil
end

#  Add new Node at end of linked list
if (self.head == nil)
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)
print("\n Empty linked list\n")
return
end

# iterating linked list elements
while (temp != nil)
print(" ", temp.data ," →")
#  Visit to next node
temp = temp.next
end

print(" NULL\n")
end

#  Print the node value at given indexes
def printIndexValue(values)
if (self.head == nil || values.head == nil)
return
end

#  Display linked list
print(" Linked List \n")
values.display()
#  Display index value
print(" Index \n")
self.display()
print("\n Index Value \n")
#  Get first node of both linked list
#  Define counter variable
counter = 1
status = false
#  iterating linked list elements
while (key != nil && value != nil)
if (counter == key.data)
status = true
print(" ", value.data)
key = key.next
end

#  Visit to next node
value = value.next
counter += 1
end

if (status == false)
#  When no index element not present
print(" None \n")
end

end

end

def main()
keys = SingleLL.new()
values = SingleLL.new()
#  Given sorted index
#  3 → 5 → 6 → 8 → NULL
#  4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
#  Find key value
keys.printIndexValue(values)
end

main()``````

#### Output

`````` Linked List
4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
Index
3 → 5 → 6 → 8 → NULL

Index Value
7 8 6 19``````
``````/*
Scala Program
Print the nodes of linked list at given indexes
*/
// Linked list node
class LinkNode(var data: Int , var next: LinkNode)
{
def this(data: Int)
{
this(data, null);
}
}
{
def this()
{
this(null, null);
}
// Add new Node at end of linked list
def addNode(data: Int): Unit = {
if (this.head == null)
{
}
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)
{
print("\n Empty linked list\n");
return;
}
//iterating linked list elements
while (temp != null)
{
print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
print(" NULL\n");
}
// Print the node value at given indexes
def printIndexValue(values: SingleLL): Unit = {
if (this.head == null || values.head == null)
{
return;
}
// Display linked list
print(" Linked List \n");
values.display();
// Display index value
print(" Index \n");
this.display();
print("\n Index Value \n");
// Get first node of both linked list
// Define counter variable
var counter: Int = 1;
var status: Boolean = false;
// iterating linked list elements
while (key != null && value != null)
{
if (counter == key.data)
{
status = true;
print(" " + value.data);
key = key.next;
}
// Visit to next node
value = value.next;
counter += 1;
}
if (status == false)
{
// When no index element not present
print(" None \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var keys: SingleLL = new SingleLL();
var values: SingleLL = new SingleLL();
// Given sorted index
// 3 → 5 → 6 → 8 → NULL
// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
// Find key value
keys.printIndexValue(values);
}
}``````

#### Output

`````` Linked List
4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
Index
3 → 5 → 6 → 8 → NULL

Index Value
7 8 6 19``````
``````/*
Swift 4 Program
Print the nodes of linked list at given indexes
*/
// Linked list node
{
var data: Int;
var next: LinkNode? ;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
}
class SingleLL
{
var tail: LinkNode? ;
init()
{
self.tail = nil;
}
// Add new Node at end of linked list
func addNode(_ data: Int)
{
if (self.head == nil)
{
}
else
{
// Append the node at last position
self.tail!.next = node;
}
self.tail = node;
}
// Display linked list element
func display()
{
if (self.head == nil)
{
print("\n Empty linked list");
return;
}
//iterating linked list elements
while (temp  != nil)
{
print("", temp!.data ,"→", terminator: "");
// Visit to next node
temp = temp!.next;
}
print(" NULL");
}
// Print the node value at given indexes
func printIndexValue(_ values: SingleLL)
{
if (self.head == nil || values.head == nil)
{
return;
}
// Display linked list
print(" Linked List ");
values.display();
// Display index value
print(" Index ");
self.display();
print("\n Index Value ");
// Get first node of both linked list
// Define counter variable
var counter: Int = 1;
var status: Bool = false;
// iterating linked list elements
while (key  != nil && value  != nil)
{
if (counter == key!.data)
{
status = true;
print(" ", value!.data, terminator: "");
key = key!.next;
}
// Visit to next node
value = value!.next;
counter += 1;
}
if (status == false)
{
// When no index element not present
print(" None ");
}
}
}
func main()
{
let keys: SingleLL = SingleLL();
let values: SingleLL = SingleLL();
// Given sorted index
// 3 → 5 → 6 → 8 → NULL
// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
// Find key value
keys.printIndexValue(values);
}
main();``````

#### Output

`````` Linked List
4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
Index
3 → 5 → 6 → 8 → NULL

Index Value
7  8  6  19``````
``````/*
Kotlin Program
Print the nodes of linked list at given indexes
*/
// Linked list node
{
var data: Int;
var next: LinkNode ? ;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
var tail: LinkNode ? ;
constructor()
{
this.tail = null;
}
// Add new Node at end of linked list
fun addNode(data: Int): Unit
{
if (this.head == null)
{
}
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)
{
print("\n Empty linked list\n");
return;
}
//iterating linked list elements
while (temp != null)
{
print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
print(" NULL\n");
}
// Print the node value at given indexes
fun printIndexValue(values: SingleLL ): Unit
{
if (this.head == null || values.head == null)
{
return;
}
// Display linked list
print(" Linked List \n");
values.display();
// Display index value
print(" Index \n");
this.display();
print("\n Index Value \n");
// Get first node of both linked list
// Define counter variable
var counter: Int = 1;
var status: Boolean = false;
// iterating linked list elements
while (key != null && value != null)
{
if (counter == key.data)
{
status = true;
print(" " + value.data);
key = key.next;
}
// Visit to next node
value = value.next;
counter += 1;
}
if (status == false)
{
// When no index element not present
print(" None \n");
}
}
}
fun main(args: Array < String > ): Unit
{
var keys: SingleLL = SingleLL();
var values: SingleLL = SingleLL();
// Given sorted index
// 3 → 5 → 6 → 8 → NULL
// 4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
// Find key value
keys.printIndexValue(values);
}``````

#### Output

`````` Linked List
4 → 8 → 7 → 3 → 8 → 6 → 10 → 19 → -5 → NULL
Index
3 → 5 → 6 → 8 → NULL

Index Value
7 8 6 19``````

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.