void print_kth_from_end(Node *head, int k){
Node *lead = head;
Node *lag = head;
while(k > 0){
if(lead){
k--;
lead = lead->next;
}
else{
std::cout << "K is larger than length of Linked List" << std::endl;
return;
}
}
while(lead){
lead = lead->next;
lag = lag->next;
}
std::cout << "kth node from end = " << lag->data << std::endl;
}
Detailed explanation can be found on Youtube:
Subscribe to KnowledgeCenter
No comments:
Post a Comment