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

Spread the post

split alternate nodes of linked list

Write an function to split alternate nodes of a linked list.

Example

Function

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

root_ptr7654321

Alternate nodes

root_ptrAlternate nodes7654321

View Nodes and pointer.

Stack Areamainhead1(pointer)head2(pointer) NULL 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)= NULL

After split.

Stack Areamainhead1(pointer)head2(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)= NULLstruct Nodedata (int)= 7next (pointer)= NULL

Try it Yourself

Program for split alternate nodes of a linked list.

Output

Visualize process.

Stack Areamainhead1(pointer)head2(pointer)splitauxiliary1(pointer)auxiliary2(pointer)head1(pointer)head2(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)= NULL

Try it Yourself

Spread the post

Recommended Posts: