Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Reverse a linked list from position M to N

Given a linked list and Reverse a linked list from position M to N. suppose linked list contain following nodes[1, 2, 3, 4, 5, 6, 7, 8, 9].

root_ptr10987654321

Try it Yourself

View pointers and nodes.

Stack Areamainroot(pointer) Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 8next (pointer) struct Nodedata (int)= 9next (pointer) struct Nodedata (int)= 10next (pointer)= NULL

Try it Yourself

Example : M=5 N=10

Before reverse.

root_ptr10987654321

After reverse.

root_ptr56789104321

visualize execution process.

Stack Areamainroot(pointer)reverse_n_to_m_nodem (int) =5n (int) =-1root(pointer) Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 5next (pointer)= NULLstruct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 8next (pointer) struct Nodedata (int)= 9next (pointer) struct Nodedata (int)= 10next (pointer)

Try it Yourself

Accepted Output

C program to Reverse M to N position nodes of linked list . Iterative approach.


Output

Spread the post

Recommended Posts: