Skip to content

Instantly share code, notes, and snippets.

@bitcpf
Created June 24, 2014 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitcpf/b66f30e9790e08189bb7 to your computer and use it in GitHub Desktop.
Save bitcpf/b66f30e9790e08189bb7 to your computer and use it in GitHub Desktop.
import java.util.Iterator;
import java.util.LinkedList;
/*
* Created by bitcpf
*/
public class Lastkey {
public static int kthnode(LinkedList<Integer> input, int k){
if (input == null || input.size()<k || k<0){
System.out.println("Entry null");
return (Integer) null;
}
Iterator<Integer> iter = input.iterator();
int i = 0;
while(i<k){
iter.next();
i++;
}
int r_index = 0;
while(iter.hasNext()){
iter.next();
r_index ++;
}
return input.get(r_index);
}
public static void main(String[] args){
LinkedList<Integer> test = new LinkedList<Integer>();
for(int i = 4; i < 23; i++){
test.add(i);
}
int k = 4;
System.out.println(test.toString());
System.out.println(kthnode(test,k));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment