-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathPrintLastK.java
51 lines (39 loc) · 1.02 KB
/
PrintLastK.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
45
46
47
48
49
50
51
package algorithm;
public class PrintLastK {
static class Node {
Node next;
int value;
public Node(int value) {
this.value = value;
}
}
private static int findLastK(Node node, int k) {
if (node == null) {
return -1;
}
Node fastPointer = node;
Node slowPointer = node;
for (int i = 0; i < k; i++) {
fastPointer = fastPointer.next;
}
while (fastPointer != null) {
fastPointer = fastPointer.next;
slowPointer = slowPointer.next;
}
return slowPointer.value;
}
public static void main(String[] args) {
Node n1 = new Node(1);
Node n2 = new Node(2);
Node n3 = new Node(3);
Node n4 = new Node(4);
Node n5 = new Node(5);
Node n6 = new Node(6);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n5;
n5.next = n6;
System.out.println(findLastK(n1, 3));
}
}