Spread the post

Move Last node at given position in linked list

Suppose linked list contain following nodes [ 10, 20, 30, 40, 50, 60, 70, 80 ]. Our goal is to Move Last node at given position of linked list.

root_ptr8070605040302010

Try it Yourself

view pointers and nodes.

Stack Areamainposition (int) ? root(pointer) Heap Areastruct Nodedata (int)= 10next (pointer) struct Nodedata (int)= 20next (pointer) struct Nodedata (int)= 30next (pointer) struct Nodedata (int)= 40next (pointer) struct Nodedata (int)= 50next (pointer) struct Nodedata (int)= 60next (pointer) struct Nodedata (int)= 70next (pointer) struct Nodedata (int)= 80next (pointer)= NULL

Try it Yourself

Example 1: Move last node at [6].

root_ptr8070605040302010Before Move Last node at position 6

After move

root_ptr7060805040302010

Visualize process.

Stack Areamainposition (int) =6root(pointer)move_last_nodehelper (int) =6move_position(pointer)position (int) =0root(pointer)temp(pointer) Heap Areastruct Nodedata (int)= 10next (pointer) struct Nodedata (int)= 20next (pointer) struct Nodedata (int)= 30next (pointer) struct Nodedata (int)= 40next (pointer) struct Nodedata (int)= 50next (pointer) struct Nodedata (int)= 60next (pointer) struct Nodedata (int)= 70next (pointer)= NULLstruct Nodedata (int)= 80next (pointer)

Try it Yourself

C program to Move Last node at given position. Iterative approach.


Output

Spread the post

Recommended Posts: