Spread the post

Delete all nodes which are less than of right side node

Write an efficient algorithm, to delete all nodes which is less than of a Right side node..

Example

Given linked list contain following nodes [8 1 7 6 5 4 2 3].

root_ptr32456718

View Nodes and pointer.

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

Try it Yourself

After delete nodes.

root_ptr345678 Stack Areamainroot(pointer) Heap Areastruct Nodedata (int)= 8next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 3next (pointer)= NULL

Program for Delete nodes which have a greater value on right side.

Output

Visualize process.

Stack Areamainroot(pointer)delete_g_rightprev(pointer)remove(pointer)root(pointer)temp(pointer) Heap Areastruct Nodedata (int)= 8next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 3next (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: