# Hackerrank Print the elements of a linked list solution

## Problem Statement

This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.

If you’re new to working with linked lists, this is a great exercise to get familiar with them. You’re given the pointer to the head node of a linked list and you need to print all its elements in order, one element per line. The head pointer may be null, i.e., it may be an empty list. In that case, don’t print anything!

## Source code:

`````` /*
Print elements of a linked list on console
head pointer input could be NULL as well for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
{
// This is a "method-only" submission.
// You only need to complete this method.
}
}
``````

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

# Hackerrank Print in Reverse solution

## Problem Statement

This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.

Youâ€™re given the pointer to the head node of a linked list and you need to print all its elements in reverse order from tail to head, one element per line. The head pointer may be null meaning that the list is empty - in that case, donâ€™t print anything!

## Soruce code:

`````` /*
Print elements of a linked list in reverse order as standard output
head pointer could be NULL as well for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
{
// This is a "method-only" submission.
// You only need to complete this method.
int a[100],i=0;
i++;
}
for(int j = i-1;j>=0;j--){
cout<<a[j]<<endl;
}
}
``````
** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

# Hackerrank Reverse a doubly linked list solution

## Problem Statement

This challenge is part of a tutorial track by MyCodeSchool

You’re given the pointer to the head node of a doubly linked list. Reverse the order of the nodes in the list. The head node might be NULL to indicate that the list is empty.

## Source Code:

`````` /*
Reverse a doubly linked list, input list may also be empty
Node is defined as
struct Node
{
int data;
Node *next;
Node *prev;
}
*/
{
Node *cur = head,*temp = new Node;
// Complete this function
// Do not write the main method.
while(cur !=NULL){
temp->next = cur->next;
temp->prev = cur->prev;
cur->next = temp->prev;
cur->prev = temp->next;
cur = temp->next;
if(cur!=NULL){
}
}
}  ``````
``````
** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

# Hackerrank Merge two sorted linked lists solution

## Problem Statement

This challenge is part of a tutorial track by MyCodeSchool

You’re given the pointer to the head nodes of two sorted linked lists. The data in both lists will be sorted in ascending order. Change the next pointers to obtain a single, merged linked list which also has data in ascending order. Either head pointer given may be null meaning that the corresponding list is empty.

## Source code:

`````` /*
Merge two sorted lists A and B as one linked list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
{
// This is a "method-only" submission.
// You only need to complete this method
Node *curA,*curB;
while(curB !=NULL){
if(cur== NULL){
break;
}else{    if(cur->data > curB->data){
curA = curB;
curB = curB->next;
curA->next = cur;
if(prev == NULL){
}else{
prev->next = curA;
}
prev = cur;
cur = cur->next;
}else{
if(cur->next !=NULL){
prev = cur;
cur = cur->next;
}else{
cur->next = curB;
break;
}
}}
}  ``````
``````
** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

# Hackerrank Reverse a linked list solution

## Problem Statement

This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.

You’re given the pointer to the head node of a linked list. Change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty.

## source code:

`````` /*
The input list will have at least one element
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
{
// Complete this method
Node *prev = NULL;
Node *next ;
while(cur!=NULL){
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
}  ``````
** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

# Hackerrank Insert a node at a specific position in a linked list solution

## Problem Statement

This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.

You’re given the pointer to the head node of a linked list, an integer to add to the list and the position at which the integer must be inserted. Create a new node with the given integer, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is empty.

## source code:

`````` /*
Insert Node at a given position in a linked list
The linked list will not be empty and position will always be valid
First element in the linked list is at position 0
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* InsertNth(Node *head, int data, int position)
{
// Complete this method only
// Do not write main function.
Node *cur = new Node,*temp = new Node;
temp->data = data;
temp->next = NULL;
}else{
int i=0;
while(cur!=NULL){
if(position == 0){
temp->next = cur;
}
else if(i==position-1){
temp->next = cur->next;
cur ->next = temp;
}else{
}i++;
cur= cur->next;
}
}
}
``````

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

# Hackerrank Detect Cycle solution

## Problem Statement

This challenge is part of a tutorial track by MyCodeSchool

You’re given the pointer to the head node of a linked list. Find whether the list contains any cycle (or loop). A linked list is said to contain cycle if any node is re-visited while traversing the list. The head pointer given may be null meaning that the list is empty.

## Source Code:

`````` /*
Detect loop in a linked list
List could be empty also
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
{
// Complete this function
// Do not write the main method
while(fast!= NULL){
fast = fast->next->next;
slow = slow->next;
if(fast == slow){
return 1;
}
}return 0;
}
``````

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

# Hackerrank Find Merge Point of Two Lists solution

## Problem Statement

This challenge is part of a tutorial track by MyCodeSchool

You’re given the pointer to the head nodes of two linked lists that merge together at some node. Find the node at which this merger happens. The two head nodes will be different and neither will be NULL.

## Source code:

`````` /*
Find merge point of two linked lists
Node is defined as
struct Node
{
int data;
Node* next;
}
*/
{
// Complete this function
// Do not write the main method.
Node *tempB;
while(tempB!=NULL){
return tempB->data;
}
tempB = tempB->next;
}
}
}
``````

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

# Hackerrank Insert a node at the tail of a linked list solution

## Problem Statement

This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.

You’re given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer, insert this node at the tail of the linked list and return the head node. The head pointer given may be null meaning that the initial list is empty.

## source code:

`````` /*
Insert Node at the end of a linked list
head pointer input could be NULL as well for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
{
// Complete this method
Node *cur= new Node;
cur->data = data;
cur->next = NULL;
}else{
Node *curr = new Node;
while(curr->next !=NULL){
curr = curr->next;
}
curr->next = cur;
}
}
``````

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

# Hackerrank Delete a Node solution

## Problem Statement

This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.

You’re given the pointer to the head node of a linked list and the position of a node to delete. Delete the node at the given position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The list may become empty after you delete the node.

## Source code:

`````` /*
Delete Node at a given position in a linked list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
{
// Complete this method
Node *cur;
if(position == 0){
}else{
int i = 0;
while(cur!= NULL){
if(i == (position)-1){
cur -> next = (cur->next)->next;
break;
}else{
cur = cur->next;
}
i++;
}
}
}
``````

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

# Hackerrank Insert a node at the head of a linked list solution

## Problem Statement

This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.

You’re given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer, insert this node at the head of the linked list and return the new head node. The head pointer given may be null meaning that the initial list is empty.

## Source Code:

`````` /*
Insert Node at the begining of a linked list
Initially head pointer argument could be NULL for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
return back the pointer to the head of the linked list in the below method.
*/
{
// Complete this method
Node *cur = new Node;
cur ->data = data;
cur ->next = NULL;
}else{
}
}
``````

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

# Hackerrank Compare two linked lists Solution

## Problem Statement

This challenge is part of a tutorial track by MyCodeSchool

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. Either head pointer given may be null meaning that the corresponding list is empty.

## source code:

`````` /*
Compare two linked lists A and B
Return 1 if they are identical and 0 if they are not.
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
{
// This is a "method-only" submission.
// You only need to complete this method
return 0;
return 0;
}
}
return 1;
}
``````

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

# Hackerrank Delete duplicate-value nodes from a sorted linked list Solution

## Problem Statement

This challenge is part of a tutorial track by MyCodeSchool
You're given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete as few nodes as possible so that the list does not contain any value more than once. The given head pointer may be null indicating that the list is empty.
For now do not be concerned with the memory deallocation. In common abstract data structure scenarios, deleting an element might also require deallocating the memory occupied by it. For an initial intro to the topic of dynamic memory please consult:http://www.cplusplus.com/doc/tutorial/dynamic/

## Source code:

`````` /*
Remove all duplicate elements from a sorted linked list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
{
// This is a "method-only" submission.
// You only need to complete this method.
while(cur->next!=NULL){
if(cur->data == cur->next->data)
cur->next = cur->next->next;
else
cur = cur->next;
}
}
``````

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)

# Hackerrank Get Node Value Solution

Problem Statement
This challenge is part of a tutorial track by MyCodeSchool
You’re given the pointer to the head node of a linked list and a specific position. Counting backwards from the tail node of the linked list, get the value of the node at the given position. A position of 0 corresponds to the tail, 1 corresponds to the node before the tail and so on.

## Solution:

`````` /*
Get Nth element from the end in a linked list of integers
Number of elements in the list will always be greater than N.
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
{
// This is a "method-only" submission.
// You only need to complete this method.
int n = 0;
while(cur!=NULL){
n++;
cur = cur->next;
}
n --;