Remove Nth Node From End of List
Linked list
medium
Score: 40
Given the head of a linked list, remove the nth node from the end of the list and return its head
.
Class Node:
data (int)
next (Node)
Input Format
First Parameter - Node head
Second Parameter - number n
Output Format
Return the Node
Example 1 :
Input :
head = 1 2 3 4 5
n = 2
Output :
1 2 3 5
Explanation :
2nd node from the end is the node with value 4, Hence it is deleted.
Example 2 :
Input :
head = 1 2
n = 1
Output :
1
Explanation :
1st node from the end is the node with value 2, Hence it is deleted.
Constraints:
- The number of nodes in the list is
sz
. - 1 <=
sz
<= 30 - 0 <=
Node.data
<= 100 - 1 <=
n
<=sz
- Expected Time Complexity: O(N).
- Expected Auxiliary Space: O(1).