Aspire Faculty ID #11692 · Topic: CUET 2024 · Just now
CUET 2024

If we want to find last node of a singly linked list then the correct coding is

Solution

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.

Correct Code Logic: while (temp → link != NULL) temp = temp → link;

Previous 10 Questions — CUET 2024

Nearest first

Next 10 Questions — CUET 2024

Ascending by ID
Ask Your Question or Put Your Review.

loading...