I understood this solution using Gfg but it is giving TLE. How can i improve the solution.
ide->https://ide.codingblocks.com/s/243527
ques->https://www.interviewbit.com/problems/reverse-link-list-ii/
I understood this solution using Gfg but it is giving TLE. How can i improve the solution.
ide->https://ide.codingblocks.com/s/243527
ques->https://www.interviewbit.com/problems/reverse-link-list-ii/
@mikkyimran hey check this pseudo code:
ListNode* Solution::reverseBetween(ListNode* A, int B, int C) {
if(B != 1){ // if B is not the first element
A->next = reverseBetween(A->next, B-1, C-1);// pass the next element decreasing B and C
return A;
}
else{ // if B is the first element
ListNode *prev=NULL, *curr=A, *next=NULL;
int count=0;
while(count<C){ // reverse the list till C
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
count++;
}
A->next = curr;// Append the first list element to the C+1 element
return prev;// return the new start
}
}
How can i optimise my current solution?
@rishabhmahajan546 can u pls explain with an example . i did a dry run but was not able to get the answer.
@mikkyimran hey I am using recursion in this code we will call recursion for head->next and it will reverse from m to n and then we have to handle the case when first element is itself m ,so for that hmne simple reverse wala logic lgaya hai iterative one from m to n.Hope you get it
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.