Created
February 11, 2013 17:49
-
-
Save charlespunk/4756120 to your computer and use it in GitHub Desktop.
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
Implement an algorithm to find the kth to last element 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
public static Node findLastKth(Node root, int Kth){ | |
if(root == null) return null; | |
Node front = root; | |
for(int i = 1; i < Kth; i++){ | |
front = front.next; | |
if(front == null) return null; | |
} | |
while(front.next != null){ | |
root = root.next; | |
front = front.next; | |
} | |
return root; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment