Skip to content

Instantly share code, notes, and snippets.

@charlespunk
Created February 11, 2013 17:49
Show Gist options
  • Save charlespunk/4756120 to your computer and use it in GitHub Desktop.
Save charlespunk/4756120 to your computer and use it in GitHub Desktop.
Implement an algorithm to find the kth to last element of a singly linked list.
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