Created
June 24, 2014 15:17
-
-
Save iwilbert/700a22f8aca7b52544f5 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
public int kthToLast(ListNode head, int k) { | |
if(head == null) | |
return -1; | |
ListNode p1, p2; | |
p1 = p2 = head; | |
for(int i = 0; p1 != null && i < k; i++) { | |
p1 = p1.next; | |
} | |
while(p1 != null) { | |
p1 = p1.next; | |
p2 = p2.next; | |
} | |
return p2.val; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment