Remove last node of the linked list

This is very simple problem are how to remove last node from singly linked list. We can solve this problem by finding the last node of linked list and after that remove this node element.

Implementing approach: Use two pointer variable of linked list node like p1 and p2. Pointer p1 are assign the NULL value and p2 are assign the address of first node of linked list. we are assume that linked list is not empty. And iterate linked list node by using of this p2 pointer.

Check p2 is not NULL and p2 pointer linked list next node reference is not NULL. Then assign p2 value to p1 pointer and p2 are visited next node of linked list.

Repeat this process until p2 next pointer are not NULL. When p2 next upcoming node are null. In this situation p2 is an deleted node last node. And p1 is holding the reference of second last node, Then p1 next node are set to NULL. and remove this p2 node.

Suppose we are inserted the following (1,2,3,4,5,6) node in a sequence.

delete last node of linked list After delete last node of linked list

Here given code implementation process.



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved