# HackerRank – DataStructure – Linked List

## HackerRank Solution Print the Elements of a Linked List

```#include <bits/stdc++.h> ...

{
}

}

int main() ...
```

## HackerRank Solution Insert a Node at the Tail of a Linked List

```#include <bits/stdc++.h> ...

Node* Insert(Node *head,int data)
{
//struct Node*temp=(struct Node*) malloc(sizeof(struct Node*));
Node *temp1=new Node;
Node *temp2=new Node;
temp1->data=data;
temp1->next=NULL;
else
{
while(temp2->next!=NULL)
temp2=temp2->next;
temp2->next=temp1;
}
}

int main() ...
```

## HackerRank Solution Insert a node at the head of a linked list

```#include <bits/stdc++.h> ...

{
Node *temp1=new Node;
temp1->data=data;
}

int main() ...
```

## HackerRank Solution Insert a node at a specific position in a linked list

```#include <bits/stdc++.h> ...

{
Node *temp1=new Node;
Node *temp2=new Node;
temp1->data=data;
temp1->next=NULL;
if(position==0)
{
}
else
{
for(int i=0;i<position-1;i++)
temp2=temp2->next;
temp1->next=temp2->next;
temp2->next=temp1;
}
}

int main() ...
```

## HackerRank Solution Delete a Node

```#include <bits/stdc++.h> ...

{
Node *temp1=new Node;
if(position==0)
{
free(temp1);
}
for(int i=0;i<position-1;i++)
temp1=temp1->next;
Node *temp2=new Node;
temp2=temp1->next;
temp1->next=temp2->next;
free(temp2);
}

int main() ...
```

## HackerRank Solution Print in Reverse

```#include <bits/stdc++.h> ...

{
int i=0;
int a[1000];
while(temp!=NULL)
{
a[i]=temp->data;
i++;
temp=temp->next;
}
for(int j=0;j<i;j++)
cout<<a[i-j-1]<<endl;
}

int main() ...
```

## HackerRank Solution Compare two linked lists

```#include <bits/stdc++.h> ...
{
Node *temp1=new Node;
Node *temp2=new Node;
while(temp1!=NULL||temp2!=NULL)
{
if((temp1==NULL&&temp2!=NULL)||(temp1!=NULL&&temp2==NULL))
return 0;
if(temp1->data!=temp2->data)
return 0;
temp1=temp1->next;
temp2=temp2->next;
}
return 1;
}

int main() ...
```

## HackerRank Solution Get Node Value

```#include <bits/stdc++.h> ...

{
int a=0,b=0,p;
Node *temp1=new Node;
while(temp1!=NULL)
{
a++;
temp1=temp1->next;
}
Node *temp2=new Node;
while(temp2!=NULL)
{
if(a-b-1==positionFromTail)
p=temp2->data;
b++;
temp2=temp2->next;
}
return p;
}

int main() ...

```

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

```#include <bits/stdc++.h> ...

if(p->next == NULL)
q = p->next;
if(p->data == q->data)
{
if(q->next==NULL)
{
p->next=NULL;
}
p->next = q->next;
r = q->next;
free(q);
if(p->data != r->data && p->next != NULL)
removeDuplicates(p->next);
if(p->data == r->data)
removeDuplicates(p);
}
else if(p->next != NULL)
removeDuplicates(p->next);
}

int main() ...
```

## HackerRank Solution Cycle Detection

```#include <bits/stdc++.h> ...

{
while(fast!=NULL)
{
fast=fast->next->next;
slow=slow->next;
if(fast==slow)
return 1;
}
return 0;
}

int main() ...
```

## HackerRank Solution Find Merge Point of Two Lists

```#include <bits/stdc++.h> ...

{
Node *tempB=new Node;
{
while(tempB!=NULL)
{
return(tempB->data);
tempB=tempB->next;
}
}
}

int main() ...
```

## HackerRank Solution Reverse a doubly linked list

```#include <bits/stdc++.h> ...

{
Node *cur=new Node,*temp=new Node;
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)