Spread the post

Delete duplicate adjacent nodes in linked list

Write an efficient to delete adjacent duplicate nodes in linked list.

Test cases

1) If linked list are empty show valid user message.

2) If similar group of adjacent node are found. Then hold first node of this group and delete other similar nodes of this pair.

3) If duplicate adjacent nodes can not found. then not delete linked list nodes.

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

root_ptr79227771

View pointers and nodes of this linked list.

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

In this linked list two group of same adjacent nodes [7 7 7 ,2 2]. view in below.

root_ptr79227771

After remove.

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

View code animation

Spread the post

Recommended Posts: