1
2
3
4
5
6
7
8
9
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
75
76
class Node {
int data;
Node nextNode;
}
class LinkedList {
private Node head;
// {...}
public void insertNth(int data, int position) {
//create new node.
Node node = new Node();
node.data = data;
node.nextNode = null;
if (this.head == null) {
//if head is null and position is zero then exit.
if (position != 0) {
return;
} else { //node set to the head.
this.head = node;
}
}
if (head != null && position == 0) {
node.nextNode = this.head;
this.head = node;
return;
}
Node current = this.head;
Node previous = null;
int i = 0;
while (i < position) {
previous = current;
current = current.nextNode;
if (current == null) {
break;
}
i++;
}
node.nextNode = current;
previous.nextNode = node;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX