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

Spread the post

Delete alternate nodes of a linked list

Every second node of linked list are alternate node. given few examples of alternate nodes.

Example 1: View Alternate Node.

Alternate linked list node

View linked list insertion process.

Try it yourself

After delete:

root_ptr8796332183

Example 2: given linked list.

root_ptr9998887776664443332221111

View insertion process.

Try it yourself

After delete nodes linked list is.

root_ptr8886663331111

Algorithm: iterative approach.

Use two pointer variable temp and free_ptr. temp pointer are use to access linked list node. And another free_ptr pointer are used to delete alternate node.

Time complexity of this algorithm is O(n).

C program to delete alternate node.

Output

Code execution: view code execution process.

Before Delete linked list.

Stack Areamainroot(pointer)show_datatemp(pointer) Heap Areastruct Nodedata (int)= 1111next (pointer) struct Nodedata (int)= 222next (pointer) struct Nodedata (int)= 333next (pointer) struct Nodedata (int)= 444next (pointer) struct Nodedata (int)= 666next (pointer) struct Nodedata (int)= 777next (pointer) struct Nodedata (int)= 888next (pointer) struct Nodedata (int)= 999next (pointer)= NULL

After Delete Alternate nodes.

Stack Areamainroot(pointer) Heap Areastruct Nodedata (int)= 1111next (pointer) struct Nodedata (int)= 333next (pointer) struct Nodedata (int)= 666next (pointer) struct Nodedata (int)= 888next (pointer)= NULL

Note that not given all step of execution process here.View How to insert Linked list node and so on.

Try it yourself

Spread the post

Recommended Posts: