Spread the post

Reverse second half of linked list

Reverse second half nodes of given linked list without using recursion.

For example Given Linked list are contain following nodes.

root_ptr87654321

Result

View pointers and nodes of linked list before reverse.

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)= NULL

After reverse half elements.

root_ptr56784321

Note that: Mofitying node link.

Stack Areamainroot(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)= NULLstruct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 8next (pointer)

Try it Yourself

Program for reverse second half nodes of given linked list. without recursion.


Output

View process

Stack Areamainroot(pointer)reverse_halfauxilary(pointer)helper(pointer)root(pointer)store(pointer)temp(pointer) Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 4next (pointer)= NULLstruct Nodedata (int)= 5next (pointer)= NULLstruct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 8next (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: