![]() ![]() Then we need to reverse the sublist starting from the m th node to the n th node. ![]() After that, we need to detach the list that is present after the n th node and save the (n+1) th node’s address in a variable.At first, we need to find the m th node.Our approach will be quite straightforward. If stuck, no problem, we will thoroughly see how we can approach this problem in the next section. Let’s see how we can approach it.īefore moving to the approach section, try to think about how you can approach this problem. Now, I think from the above examples, the problem statement is clear. So in this case, after reversing the sublist from position 2 to 3 (both inclusive), our final output list will be 9→8→4→7→1→NULL.Let’s take another example, let the input be 9→4→8→7→1→NULL, m = 2, n = 3. Our final output linked list after reversing the sublist is:.The sublist from position 3 to position 5 is 1→7→4, so we need to reverse this sublist of the given linked list.According to the problem statement, we need to reverse the given list from position m=3 to position n=5, i.e., from 3 rd node to 5 th node (both inclusive).Let’s try to understand this problem with the help of examples. We need to reverse the list from position m to n and leave the rest of the list as it is. Given a linked list and two integers (say ‘m’ and ‘n’). Having a good grasp on linked list can be a huge plus point in coding interviews. The linked list is one of the most important concepts to know while preparing for interviews. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |