We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
However, the Input Format description forbids this case. The key line is:
"position will always be at least 0 and less than the number of the elements in the list."
(i.e. 0 <= position < length).
Assume the head is null. This means there are 0 elements in the list. Thus, position < length = 0. Since we know position >= 0, this is a contradiction. So in a valid input, the head cannot be null.
Additionally, when position > 0, Delete is called recursively. In the recursive subproblem created, both position and the number of elements are decreased by 1. In the current call, we know 0 < position < length. This implies 0 <= (position - 1) < (length - 1). So the parameters to the recursive call will be valid inputs too.
TLDR: The requirement that position < length implies that position will always reach 0 before length does (which is when head would become null).
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Delete a Node
You are viewing a single comment's thread. Return to all comments →
You're right!
However, the Input Format description forbids this case. The key line is:
"position will always be at least 0 and less than the number of the elements in the list." (i.e. 0 <= position < length).
Assume the head is null. This means there are 0 elements in the list. Thus, position < length = 0. Since we know position >= 0, this is a contradiction. So in a valid input, the head cannot be null.
Additionally, when position > 0, Delete is called recursively. In the recursive subproblem created, both position and the number of elements are decreased by 1. In the current call, we know 0 < position < length. This implies 0 <= (position - 1) < (length - 1). So the parameters to the recursive call will be valid inputs too.
TLDR: The requirement that position < length implies that position will always reach 0 before length does (which is when head would become null).