Spread the post

Delete nodes greater than specified value from linked list

write an efficient algorithm to Delete nodes greater than specified value from linked list.

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

Specified Node value [4]. After delete nodes.

root_ptr3241

View nodes and pointer.

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

Program for delete all nodes greater than specified value (x) from given linked list.

Output

Visualize process.

Stack Areamainroot(pointer)delete_nodeprev(pointer) NULLremove(pointer)root(pointer)temp(pointer)x (int) =4 Heap Areastruct Nodedata (int)= 8next (pointer)= NULLstruct 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

Spread the post

Recommended Posts: