-
-
Save pchong90/1f91c296ccfb81a8b639 to your computer and use it in GitHub Desktop.
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
#include <list> | |
#include <iostream> | |
using namespace std; | |
/* | |
use the std::list for convenience, avoid using list::end() iterator | |
need to implement my own list class later | |
*/ | |
/* | |
Time: O(N) | |
Space: O(1) | |
*/ | |
template<typename T> | |
typename list<T>::const_iterator reverse_find(const list<T> &li, size_t k){ | |
typename list<T>::const_iterator i, j; | |
i = li.cbegin(); | |
j = li.cbegin(); | |
for ( size_t a = 0; a < k; a++){ | |
i++; | |
} | |
for (; i != li.cend(); i++){ | |
j++; | |
} | |
return j; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment