-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathProblem_02.java
60 lines (54 loc) · 1.39 KB
/
Problem_02.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
52
53
54
55
56
57
58
59
60
/**
* Cracking-The-Coding-Interview
* Problem_02.java
*/
package com.deepak.ctci.Ch02_LinkedLists;
import com.deepak.ctci.Library.LinkedListNode;
/**
* <br> Problem Statement :
*
* Implement an algorithm to find K'th to
* last element of a singly linked list.
* Note : Size of linked list is not known
*
* </br>
*
* @author Deepak
*/
public class Problem_02 {
/* One Approach could be looping through the list twice.
* In first pass, loop through entire list and find the size.
* In second pass, loop through size - K times. */
/**
* Method to find K'th to last element
*
* Time Complexity : O(n)
* Space Complexity : O(1)
*
* @param head
* @param k
* @return {@link LinkedListNode}
*/
public static <T> LinkedListNode<T> findKthToLastElement(LinkedListNode<T> head, int k) {
/* If head is null or K is negative, stop processing */
if (head == null || k <= 0) {
throw new IllegalArgumentException("Invalid Input!!");
}
/* Create two pointers */
LinkedListNode<T> pointer1 = head, pointer2 = head;
/* Move first pointer till K */
for (int i = 0; i < k; i++) {
if (pointer1 == null) {
return null;
}
pointer1 = pointer1.next;
}
/* Now move both at same pace, when pointer1 hits end,
* pointer2 will be at right element */
while (pointer1 != null) {
pointer1 = pointer1.next;
pointer2 = pointer2.next;
}
return pointer2;
}
}