Give an algorithm for finding the ith-to-last node in a singly linked list in which the last node is indicated by a null next reference.
Algorithm for finding the ith-to-last node in a singly linked list:
- Initialize two pointers, current and iThToLast, to point to the head of the linked list.
- Move the current pointer i positions ahead, where i is the desired distance from the last node to the i-th-to-last node.
- If the current pointer reaches the end of the linked list (i.e., it becomes null), the desired i-th-to-last node does not exist. Return null or an appropriate error indication.
- Otherwise, move both the current and iThToLast pointers one step at a time until the current pointer reaches the end of the linked list.
- When the current pointer reaches the end of the linked list, the iThToLast pointer will be pointing to the desired i-th-to-last node.
- Return the iThToLast node.
The algorithm works by maintaining a distance of i nodes between the current and iThToLast pointers. As the current pointer moves i positions ahead, the iThToLast pointer also moves one position at a time. When the current pointer reaches the end of the linked list, the iThToLast pointer will be pointing to the i-th-to-last node.
Here is an example implementation of the algorithm in C++:
#include <iostream>
struct Node {
int data;
Node* next;
};
Node findIthToLastNode(Node head, int i) {
Node* current = head;
Node* iThToLast = head;
// Move the current pointer i positions ahead
for (int j = 0; j < i; j++) {
if (current == nullptr) {
// i is greater than the length of the linked list
return nullptr;
}
current = current->next;
}
// Move both pointers one step at a time until the current pointer reaches the end
while (current != nullptr) {
current = current->next;
iThToLast = iThToLast->next;
}
return iThToLast;
}
int main() {
// Create a sample linked list: 1 -> 2 -> 3 -> 4 -> 5 -> nullptr
Node* head = new Node{1, new Node{2, new Node{3, new Node{4, new Node{5, nullptr}}}}};
int i = 2; // Find the 2nd-to-last node
Node* iThToLastNode = findIthToLastNode(head, i);
if (iThToLastNode != nullptr) {
std::cout << "The " << i << "-th to last node is: " << iThToLastNode->data << std::endl;
} else {
std::cout << "The " << i << "-th to last node does not exist." << std::endl;
}
// Clean up the memory
Node* current = head;
while (current != nullptr) {
Node* next = current->next;
delete current;
current = next;
}
return 0;
}
This implementation creates a sample linked list and finds the i-th-to-last node using the findIthToLastNode
function. The Node
struct represents a node in the linked list, with data
storing the data value and next
pointing to the next node in the list. The function returns a pointer to the i-th-to-last node or nullptr if it does not exist.