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

Spread the post

Find second last element in linked list

Suppose given linked list contain following nodes [7, 8, 6, 1, 7, 4, 2, 9].

root_ptr92471687

View insertion process.

Try it yourself

Second last element of this linked list is [2].

root_ptr92471687

View insertion process.

Try it yourself


Algorithm to find second last node.

Use one pointer variable temp. this temp pointer point to first node of linked list. check if temp!=NULL and temp->next!=NULL and temp->next->next!=NULL then move to next memory block. Otherwise we are getthing second last node.

c program to find second last node of given linked list.


Output

Code execution: view code execution process.

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

Note that not given all step of execution process here. view more.

Try it yourself

Spread the post

Recommended Posts: