Spread the post

Move similar group of all node at beginning of linked list

Write an efficient algorithm to Move similar group of all node at beginning of linked list.

Example

For example suppose given linked list contain following nodes [7 7 7 1 1 8 2 9 9 7 ].

root_ptr7992811777 Stack Areamainroot(pointer) Heap Areastruct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 8next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 9next (pointer) struct Nodedata (int)= 9next (pointer) struct Nodedata (int)= 7next (pointer)= NULL

After move.

root_ptr7992811777 Stack Areamainroot(pointer) Heap Areastruct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 8next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 9next (pointer) struct Nodedata (int)= 9next (pointer) struct Nodedata (int)= 7next (pointer)= NULL

View code animation

Program for move all similar pair nodes at beginning of linked list. Time complexity O(n).

Output

Submit your solution in comment section.

Spread the post

Recommended Posts: