Skip to content

Instantly share code, notes, and snippets.

@leepro
Created September 2, 2013 02:55
Show Gist options
  • Save leepro/6408851 to your computer and use it in GitHub Desktop.
Save leepro/6408851 to your computer and use it in GitHub Desktop.
Mth from the end of a singly linked list.
class Solution {
int list_length = 0;
int foundValue = -1;
public int solution(IntList L, int M) {
int ret = travel2end(L,M);
if(ret == list_length) return -1;
else return foundValue;
}
public int travel2end(IntList L, int M)
{
if(L.next == null) return 1;
list_length++;
int tot = travel2end(L.next, M)+1;
if(tot == M) foundValue = L.value;
return tot;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment