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

Spread the post

Reverse middle N-nodes in linked list

You know about how to reverse linked list. In this post we are discussing about how to reverse middle N-nodes of given size. lets view few examples of this problem.

Example 1:

Visualize this linked list.

root_ptr10987654321

View insertion process Try it yourself.

Suppose [N=6].

root_ptr10987654321N=6 Reverse 6 middle element

After reverse.

root_ptr10934567821

Example 2

Test cases:


C Program to reverse middle N-nodes.


Output


Code execution: View code execution process.

Before delete linked list.

Stack Areamainreverse_node (int) ? root(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

And after reverse

Stack Areamainreverse_node (int) =3root(pointer)show_datatemp(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

Note that not given all step of execution process here. view more execution process.

Try it yourself

Spread the post

Recommended Posts: