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

Spread the post

Delete middle N nodes of linked list

Delete middle N nodes of given linked list. Given a few examples to the this problem and visualize situation to delete node.

Suppose following data are inserted on linked list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].

root_ptr121110987654321

View insertion process.

Try it Yourself

Example 1 N=6 [Delete 6 middle elements].

View middle 6 node.green rectangle represent middle node.

root_ptr121110987654321

Output After delete.

root_ptr121110321

Example 2: N=3

Given linked list.

root_ptr121110321

In this case N=3. two possibility are showing to delete middle node [2,3,10] or [2,10,11]. choose any one of them [2,3,10] or [2,10,11] and remove it.

root_ptr12N=3 Two possibility to delete [2,3,10] or [3,10,11] 1110321

But try to delete first set of nodes.

After delete.

root_ptr12111

Test case


C program to delete given middle nodes.


Output


Code execution: View code execution process.

Stack Areamaindelete_node (int) =6root(pointer)delete_n_nodecount (int) ? remove(pointer) NULLsize (int) =6temp(pointer)count_nodecount (int) =11temp(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) struct Nodedata (int)= 11next (pointer) struct Nodedata (int)= 12next (pointer)= NULL

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

Try it yourself

Spread the post

Recommended Posts: