Skip to content

Commit 31847ce

Browse files
committed
Kth node from last
1 parent d4c007b commit 31847ce

File tree

4 files changed

+138
-34
lines changed

4 files changed

+138
-34
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.ctci.linkedlists;
2+
3+
import static com.ctci.linkedlists.Node.printList;
4+
5+
/**
6+
* @author rampatra
7+
* @since 21/11/2018
8+
*/
9+
public class KthToLastElement {
10+
11+
/**
12+
* Finds the kth node from the end in a linked list.
13+
*
14+
* @param head is the reference to the head of the linked list
15+
* @param k is the position of element from the end
16+
* @return the Kth node from the end
17+
*/
18+
private static Node getKthToLastElement(Node head, int k) {
19+
Node slow = head;
20+
Node fast = head;
21+
int i = 0;
22+
23+
// forward the fast reference k times
24+
while (i < k) {
25+
if (fast == null) return null; // k is out of bounds
26+
fast = fast.next;
27+
i++;
28+
}
29+
30+
while (fast != null) {
31+
slow = slow.next;
32+
fast = fast.next;
33+
}
34+
return slow;
35+
}
36+
37+
public static void main(String[] args) {
38+
Node l1 = new Node(1);
39+
l1.next = new Node(6);
40+
l1.next.next = new Node(3);
41+
l1.next.next.next = new Node(4);
42+
l1.next.next.next.next = new Node(5);
43+
l1.next.next.next.next.next = new Node(7);
44+
printList(l1);
45+
System.out.println("k=2: " + getKthToLastElement(l1, 2).val); // NPE check is omitted intentionally to keep it simple
46+
47+
Node l2 = new Node(1);
48+
l2.next = new Node(6);
49+
l2.next.next = new Node(2);
50+
l2.next.next.next = new Node(3);
51+
l2.next.next.next.next = new Node(4);
52+
l2.next.next.next.next.next = new Node(7);
53+
printList(l2);
54+
System.out.println("k=1: " + getKthToLastElement(l2, 1).val);
55+
56+
Node l3 = new Node(1);
57+
l3.next = new Node(6);
58+
l3.next.next = new Node(3);
59+
l3.next.next.next = new Node(3);
60+
l3.next.next.next.next = new Node(4);
61+
l3.next.next.next.next.next = new Node(7);
62+
printList(l3);
63+
System.out.println("k=6: " + getKthToLastElement(l3, 6).val);
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.ctci.linkedlists;
2+
3+
/**
4+
* @author rampatra
5+
* @since 21/11/2018
6+
*/
7+
class Node {
8+
int val;
9+
Node next;
10+
11+
Node(int val) {
12+
this.val = val;
13+
}
14+
15+
public static void printList(Node head) {
16+
if (head == null) return;
17+
18+
Node curr = head;
19+
while (curr.next != null) {
20+
System.out.print(curr.val + "->");
21+
curr = curr.next;
22+
}
23+
System.out.println(curr.val);
24+
}
25+
}

src/main/java/com/ctci/linkedlists/RemoveDuplicates.java

+3-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.HashSet;
44
import java.util.Set;
55

6+
import static com.ctci.linkedlists.Node.printList;
7+
68
/**
79
* @author rampatra
810
* @since 21/11/2018
@@ -34,17 +36,6 @@ private static void removeDuplicatesFromUnsortedList(Node head) {
3436
}
3537
}
3638

37-
private static void printList(Node head) {
38-
if (head == null) return;
39-
40-
Node curr = head;
41-
while (curr.next != null) {
42-
System.out.print(curr.val + "->");
43-
curr = curr.next;
44-
}
45-
System.out.println(curr.val);
46-
}
47-
4839
public static void main(String[] args) {
4940
Node l1 = new Node(1);
5041
l1.next = new Node(2);
@@ -82,13 +73,4 @@ public static void main(String[] args) {
8273
System.out.print("Without dups: ");
8374
printList(l3);
8475
}
85-
}
86-
87-
class Node {
88-
int val;
89-
Node next;
90-
91-
Node(int val) {
92-
this.val = val;
93-
}
94-
}
76+
}

src/main/java/com/rampatra/linkedlists/NthNodeFromLast.java

+45-13
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
/**
77
* Created by IntelliJ IDEA.
88
*
9-
* @author: ramswaroop
10-
* @date: 6/18/15
11-
* @time: 6:49 PM
9+
* @author rampatra
10+
* @since 6/18/15
1211
*/
1312
public class NthNodeFromLast {
1413

14+
/**
15+
* @param list
16+
* @param n
17+
* @param <E>
18+
* @return
19+
*/
1520
public static <E extends Comparable<E>> SingleLinkedNode<E> getNthNodeFromLast(SingleLinkedList<E> list, int n) {
1621
SingleLinkedNode<E> slow = list.getNode(0);
1722
SingleLinkedNode<E> fast = list.getNode(0);
@@ -29,15 +34,42 @@ public static <E extends Comparable<E>> SingleLinkedNode<E> getNthNodeFromLast(S
2934

3035
public static void main(String a[]) {
3136
SingleLinkedList<Integer> linkedList = new SingleLinkedList<>();
32-
linkedList.add(00);
33-
linkedList.add(11);
34-
linkedList.add(22);
35-
linkedList.add(33);
36-
linkedList.add(44);
37-
linkedList.add(55);
38-
linkedList.add(66);
39-
linkedList.add(77);
40-
linkedList.add(88);
41-
System.out.println(getNthNodeFromLast(linkedList, 3).item);
37+
linkedList.add(0);
38+
linkedList.add(1);
39+
linkedList.add(2);
40+
linkedList.add(3);
41+
linkedList.add(4);
42+
linkedList.add(5);
43+
linkedList.add(6);
44+
linkedList.add(7);
45+
linkedList.add(8);
46+
linkedList.printList();
47+
System.out.println("n=3: " + getNthNodeFromLast(linkedList, 3).item);
48+
49+
SingleLinkedList<Integer> linkedList2 = new SingleLinkedList<>();
50+
linkedList2.add(0);
51+
linkedList2.add(1);
52+
linkedList2.add(2);
53+
linkedList2.add(3);
54+
linkedList2.add(4);
55+
linkedList2.add(5);
56+
linkedList2.add(6);
57+
linkedList2.add(7);
58+
linkedList2.add(8);
59+
linkedList2.printList();
60+
System.out.println("n=1: " + getNthNodeFromLast(linkedList2, 1).item);
61+
62+
SingleLinkedList<Integer> linkedList3 = new SingleLinkedList<>();
63+
linkedList3.add(0);
64+
linkedList3.add(1);
65+
linkedList3.add(2);
66+
linkedList3.add(3);
67+
linkedList3.add(4);
68+
linkedList3.add(5);
69+
linkedList3.add(6);
70+
linkedList3.add(7);
71+
linkedList3.add(8);
72+
linkedList3.printList();
73+
System.out.println("n=9: " + getNthNodeFromLast(linkedList3, 9).item);
4274
}
4375
}

0 commit comments

Comments
 (0)