-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNthNodeFromEnd.java
74 lines (67 loc) · 1.71 KB
/
NthNodeFromEnd.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package datastructure.list.program;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* Given a Linked List and a number n, write a function that returns the value
* at the n’th node from end of the Linked List.
*
* @author skedia
*
*/
public class NthNodeFromEnd {
static int i = 0;
static int data = 0;
public static void main(String[] args) {
// list to perform on
List<Integer> intList = getLinkedList();
// index from end
int n = 5;
Iterator<Integer> intListIterator = intList.iterator();
getNthNodeFromEnd(intListIterator, n);
System.out.println(data);
}
/**
* Returns the data of nth node from end of a list
*
* @param intListIterator
* @param n
*/
private static void getNthNodeFromEnd(Iterator<Integer> intListIterator, int n) {
// if it is the last node of List make index = 1 and return
if (!intListIterator.hasNext()) {
i = 1;
return;
}
// assign the node value to a temp variable
int temp = intListIterator.next();
// recurse the list to the end
getNthNodeFromEnd(intListIterator, n);
// if i equals n then assign node value in temp variable to data
if (i == n)
data = temp;
// increment i and return
i++;
return;
}
/**
* Return a pre-constructed list to perform on.
*
* @return List
*/
private static List<Integer> getLinkedList() {
List<Integer> intList = new LinkedList<Integer>();
intList.add(4);
intList.add(8);
intList.add(2);
intList.add(9);
intList.add(5);
intList.add(1);
intList.add(12);
intList.add(10);
intList.add(3);
intList.add(32);
intList.add(11);
return intList;
}
}