Skip to content

Instantly share code, notes, and snippets.

@abhi1010
Created June 8, 2014 07:46
Show Gist options
  • Save abhi1010/43e289a9877fb9293680 to your computer and use it in GitHub Desktop.
Save abhi1010/43e289a9877fb9293680 to your computer and use it in GitHub Desktop.
Kth element in Linked List
Node* findKthToLastElement (Node* node, unsigned short k)
{
Node* secondRunner = node;
for(unsigned short i = 0; i < k; ++i)
{
if (secondRunner->next != NULL)
secondRunner = secondRunner->next;
else
return NULL;
}
while (secondRunner)
{
secondRunner = secondRunner->next;
node = node->next;
}
return node;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment