Problem Description
Given a linked list A, remove the B-th node from the end of the list and return its head.
For example, Given linked list: 1->2->3->4->5
, and B = 2
. After removing the second node from the end, the linked list becomes 1->2->3->5
.
NOTE: If B is greater than the size of the list, remove the first node of the list.
NOTE: Try doing it using constant additional space.
1 <= |A| <= 106
The first argument of input contains a pointer to the head of the linked list.
The second argument of input contains the integer B.
Return the head of the linked list after deleting the B-th element from the end.
Input 1:
A = [1, 2, 3, 4, 5] B = 2
Input 2:
A = [1] B = 1
Output 1:
[1, 2, 3, 5]
Output 2:
[]
Explanation 1:
In the first example, 4 is the second last element.
Explanation 2:
In the second example, 1 is the first and the last element.
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a question? Checkout Sample Codes for more details.