Skip to content

Instantly share code, notes, and snippets.

@pchong90

pchong90/2_2.cpp Secret

Created June 27, 2014 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pchong90/1f91c296ccfb81a8b639 to your computer and use it in GitHub Desktop.
Save pchong90/1f91c296ccfb81a8b639 to your computer and use it in GitHub Desktop.
#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