## Delete a Node Hackerrank

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.

**Input Format **

You have to complete the Node* Delete(Node* head, int position) method which takes two arguments – the head of the linked list and the position of the node to delete. You should NOT read any input from stdin/console. position will always be at least 0 and less than the number of the elements in the list.

**Output Format **

Delete the node at the given position and return the head of the updated linked list. Do NOT print anything to stdout/console.

**Sample Input**

1 –> 2 –> 3 –> NULL, position = 0

1 –> NULL , position = 0

**Sample Output**

2 –> 3 –> NULL

NULL

**Explanation **

1. 0^{th} position is removed, 1 is deleted from the list.

2. Again 0^{th}position is deleted and we are left with empty list.

For learning more visit it : https://coderinme.com/

##### Solution of Delete a Node Hackerrank

/* Delete Node at a given position in a linked list Node is defined as struct Node { int data; struct Node *next; } */ Node* Delete(Node *head, int position) { // Complete this method Node *prev = NULL; Node *ptr = head; int pos = 0; if(position==0) { head=head->next; delete (ptr); } else { while(position!=pos) { ++pos; prev=ptr; ptr=ptr->next; } if(ptr!=NULL) { prev->next=ptr->next; delete (ptr); } } return head; }

/* Delete Node at a given position in a linked list Node is defined as struct Node { int data; struct Node *next; } */ Node* Delete(Node *head, int position) { if (position == 0){ return head->next; } head->next = Delete(head->next, position-1); return head; // Complete this method }

```
/*
Insert Node at the end of a linked list
head pointer input could be NULL as well for empty list
Node is defined as
class Node {
int data;
Node next;
}
*/
// This is a "method-only" submission.
// You only need to complete this method.
Node Delete(Node head, int position) {
// Complete this method
if(head==null)
return null;
Node temp=head;
Node prev=null;
if(position==0)
{
head=head.next;
temp.next=null;
return head;
}
while(position > 0 )
{
prev=temp;
temp=temp.next;
position--;
}
prev.next=temp.next;
temp.next=null;
return head;
}
```

```
"""
Delete Node at a given position in a linked list
Node is defined as
class Node(object):
def __init__(self, data=None, next_node=None):
self.data = data
self.next = next_node
return back the head of the linked list in the below method.
"""
def Delete(head, position):
if head is None:
return head
elif position == 0:
return head.next
prev_node = head
current_node = head
current_position = 0
while current_position < position:
current_position = current_position + 1
prev_node = current_node
current_node = current_node.next
prev_node.next = current_node.next
return head
```