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

Spread the post

Arrange linked list around given value x

Write an efficient algorithm to Arrange linked list around given value x. note that x is an integer value. arrange the node which are satisfied following condition.
1) All less than value of x is left side.
2) And arrange all node to x in right side which are gratore than or equal to x.

Example

Function

Given linked list contain following nodes [1 7 6 2 9 5 11 ].

root_ptr11592671

View Nodes and pointer.

Stack Areamainroot(pointer)x (int) =6 Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 9next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 11next (pointer)= NULL

Given X=6. After arrange.

root_ptr11976521

Try it Yourself

change the node link.

Stack Areamainroot(pointer)x (int) =6 Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 9next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 11next (pointer)= NULL

Program for Arrange a linked list around a given value x.

Output

Visualize process.

Stack Areamainroot(pointer)x (int) =6arrangeauxiliary(pointer)large(pointer)root(pointer)second(pointer)smaller(pointer)temp(pointer) NULLx (int) =6 Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 9next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 11next (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: