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

Spread the post

Reverse a Given linked list

Given an unsorted linked list. Reverse a linked list. suppose linked list contain are following nodes [1, 2, 3, 4, 5, 6, 7, 8].

root_ptr87654321

View insertion process.

Try it Yourself

After reverse a linked list.

root_ptr12After reverse345678

Accepted Output


Algorithm:


Time complexity of this program O(n).

C program to reverse given linked list.


Output


Code execution

Stack Areamainroot(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) struct Nodedata (int)= 8next (pointer)= NULL

After reverse.

Stack Areamainroot(pointer) Heap Areastruct Nodedata (int)= 1next (pointer)= NULLstruct 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) struct Nodedata (int)= 8next (pointer)

Note given all execution process.

Try it Yourself

Spread the post

Recommended Posts: