# Delete a Node

### Problem Statement :

```Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value.

Example:
list=0->1->2->3
position=2

After removing the node at position 2, list'= 0->1->-3.

Function Description:

Complete the deleteNode function in the editor below.

deleteNode has the following parameters:
- SinglyLinkedListNode pointer llist: a reference to the head node in the list
- int position: the position of the node to remove

Returns
- SinglyLinkedListNode pointer: a reference to the head of the modified list

Input Format:

The first line of input contains an integer n, the number of elements in the linked list.
Each of the next n lines contains an integer, the node data values in order.
The last line contains an integer, position, the position of the node to delete.

Constraints:
1.  1<=n<=1000
2.  1<=list[i]<=1000```

### Solution :

```                            ```Solution in C :

In C:

//the following function is all that is needed to complete
// the challenge in hackerrank platform.

if((position) == 0) {
return llist->next;
}
llist->next = deleteNode(llist->next, position-1);
return llist;
}

In C++:

//the following function is all that is needed to complete
//the challenge in hackerrank platform

{
// Complete this method
Node *prev = NULL;

int pos = 0;

if(position==0)
{
delete (ptr);
}
else
{
while(position!=pos)
{
++pos;
prev=ptr;
ptr=ptr->next;
}

if(ptr!=NULL)
{
prev->next=ptr->next;
delete (ptr);
}
}
}

In Java:

//the following method is all that is needed to complete the
//challenge in hackerrank plartform

int currentNodePosition = 0;

if (position == 0) {
}

while (currentNodePosition < position - 1) {
currentNode = currentNode.next;
currentNodePosition++;
}

if (currentNode.next != null && currentNode.next.next != null) {
currentNode.next = currentNode.next.next;
}

}

In Python 3:

#the following method is all that is needed to complete the
#challenge in hackerrank platfrom

currentPos = 0
prevNode = None
while currentPos < position:
currentPos = currentPos+1
prevNode = node
node = node.next

if prevNode is not None:
prevNode.next = node.next
else:
n = node.next
node.next = None
return n```
```

## View More Similar Problems

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

## Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

## Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

## Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

## Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

## Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle