Search This Blog

Wednesday, January 1, 2014

Algorithm - Linked Lists - Delete a node from a linked list

Delete a node from a linked list

The following is the solution to Hacker Rank problem Delete a node from a linked list using Java.  For solutions to other Hacker Rank Problem visit my page HackerRank, alternatively try searching for the problem in my blog.

Score: 5/5
Node* Delete(Node *head, int position)
{
    // Complete this method
    Node *temp = head;
    if(position == 0)
        head = head->next;
    else
    {
        for(int i = 1; i < position; i++)
            temp = temp->next;
        Node *del = temp->next;
        temp->next = del->next;
    }
    return head;
}

No comments:

Post a Comment

Labels