Created
September 2, 2013 02:55
-
-
Save leepro/6408851 to your computer and use it in GitHub Desktop.
Mth from the end of a singly linked list.
This file contains hidden or 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
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