-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKElementLast.java
More file actions
41 lines (37 loc) · 827 Bytes
/
KElementLast.java
File metadata and controls
41 lines (37 loc) · 827 Bytes
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
class Node {
Node next;
int data;
Node(int d) {
data = d;
next = null;
}
}
public class KElementLast {
public static void main(String[] args) {
KElementLast obj = new KElementLast();
Node root = new Node(1);
obj.addNodes(root, 2,3,4,5,6,7,8,9);
System.out.println(obj.getKthFromLast(root, 2));
}
int getKthFromLast(Node head, int k) {
Node fast = head;
Node slow = head;
for (int i=0;i<k;i++) {
if (fast==null) return -1;
fast = fast.next;
}
while (fast!=null) {
fast=fast.next;
slow= slow.next;
}
return slow.data;
}
private void addNodes(Node root, int... nums) {
Node temp = root;
for (int i=0;i<nums.length;i++) {
Node newNode = new Node(nums[i]);
temp.next = newNode;
temp = temp.next;
}
}
}