HackerRank – DataStructure – Linked List

HackerRank Solution Print the Elements of a Linked List

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

void printLinkedList(SinglyLinkedListNode* head) {
 while(head!=NULL)
    {
         cout <<head->data <<endl;
         head=head->next;
    }

}

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;
    if(head==NULL)
        head=temp1;
    else
    {  
    temp2=head;
    while(temp2->next!=NULL)
        temp2=temp2->next;
    temp2->next=temp1;
    }
    return head;
}

int main() ...

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

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

SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* llist, int data)
{
    Node *temp1=new Node;
    temp1->data=data;
    temp1->next=head;
    head=temp1;
    return head;
}

int main() ...

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

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

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

int main() ...

HackerRank Solution Delete a Node

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

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

int main() ...

HackerRank Solution Print in Reverse

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

void reversePrint(SinglyLinkedListNode* head)
{
    int i=0;
    Node *temp=head;
    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> ...
bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2)
{
    Node *temp1=new Node;
    Node *temp2=new Node;
    temp1=headA;
    temp2=headB;
    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 getNode(SinglyLinkedListNode* head, int positionFromTail)
{
    int a=0,b=0,p;
    Node *temp1=new Node;
    temp1=head;
    while(temp1!=NULL)
    {
        a++;    
        temp1=temp1->next;
    }
    Node *temp2=new Node;
    temp2=head;
    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> ...

SinglyLinkedListNode* removeDuplicates(SinglyLinkedListNode* head) {
    struct SinglyLinkedListNode* p;
    struct SinglyLinkedListNode* q;
    struct SinglyLinkedListNode* r;
   
    p = head;
     if(p->next == NULL)
        return head;
    q = p->next;
    if(p->data == q->data)
    {
        if(q->next==NULL)
        {
            p->next=NULL;
            return head;
        }
        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);
    return head;
}

int main() ...

HackerRank Solution Cycle Detection

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

bool has_cycle(SinglyLinkedListNode* head)
{
    Node *fast=head,*slow=head;
    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> ...

int findMergeNode(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2)
{
    Node *tempB=new Node;
    while(headA!=NULL)
    {
        tempB=headB;  
        while(tempB!=NULL)
        {
            if(tempB==headA)
                return(tempB->data);
            tempB=tempB->next;
        }
        headA=headA->next;
    }
    return headA->data;
}

int main() ...

HackerRank Solution Reverse a doubly linked list

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

DoublyLinkedListNode* reverse(DoublyLinkedListNode* head)
{
    Node *cur=new Node,*temp=new Node;
    cur=head;
    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)
            head=cur;
    }
    return head;
}

int main() ...
%d bloggers like this: