If we want to find last node of a singly linked list then the correct coding is
🎥 Video solution / Text Solution of this question is given below:
To find the last node of a singly linked list, we must keep moving until the link (next pointer) becomes NULL. This ensures we stop exactly at the last node.
// Structure of node
struct Node {
int data;
struct Node* link;
};
// Function to get last node
struct Node* getLastNode(struct Node* head) {
struct Node* temp = head;
// Traverse until last node
while (temp -> link != NULL) {
temp = temp -> link;
}
return temp; // Now temp points to last node
}
Explanation:
Initialize a pointer temp with head.
Keep moving forward using temp = temp → link while temp → link != NULL.
When loop ends, temp is pointing to the last node.