-
Notifications
You must be signed in to change notification settings - Fork 0
/
MthLastInLinkedList.java
44 lines (40 loc) · 1.37 KB
/
MthLastInLinkedList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.io.*;
import java.util.*;
public class MthLastInLinkedList {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
String line = "Start";
BufferedReader stream = new BufferedReader(new InputStreamReader(System.in));
Long M = 0L;
Long input_count = 0L;
LinkedList<String> list;
while(true) {
input_count++;
try {
line = stream.readLine();
} catch(IOException e) {
break;
}
if(line == null) break;
if(input_count%2 != 0) M = Long.parseLong(line);
if(input_count%2 == 0) {
list = new LinkedList<String>();
for(String item : line.split(" ")) {
list.add(item);
}
Iterator<String> itr = list.descendingIterator();
String output = "NIL";
Long i = 0L;
while(itr.hasNext()) {
i++;
if(M == i) {
output = itr.next();
} else {
itr.next();
}
}
System.out.println(output);
}
}
}
}