Created
June 8, 2014 07:46
-
-
Save abhi1010/43e289a9877fb9293680 to your computer and use it in GitHub Desktop.
Kth element in Linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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