The code is showing segmentation fault on IDE, but I believe my logic is correct. Kindly help.
Linked list k Reverse
the problem is in this part of the code:
for (int i=0; i<(2*k)-1; i++){
1. sublast = sublast->next;
2. if (sublast->next==NULL){…
…
if sublast becomes NULL in line 1, accessing sublast->next will give segmentation fault.
I made correction in your code. there is no requirement of doing this part of code(accessing (2k)th node of the list and pointing it by tail of so far reversed list ).
you can do it in a more better way.
like this: say f(x,k) return head of the reverse of linked list with x elements.
then f(x,k) =
{1. reverse first k element
2. tail = tail of this reversed list(which is original head)
3. tail->next = f(x-k,k)
}
Moreover the code that reverse k elements has problems. it will go infinitely. try to check that with some example. I fixed that part as well.
corrected code: https://ide.codingblocks.com/s/214659
please rate and resolve if satisfied.
thanks
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.