-
Notifications
You must be signed in to change notification settings - Fork 17
/
NthPointerFromENd.java
70 lines (67 loc) · 1.55 KB
/
NthPointerFromENd.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
package SummerTrainingGFG.LinkedList;
/**
* @author Vishal Singh
*/
class Node2{
int data;
Node2 next;
Node2(int data){
this.data = data;
}
}
class List2{
Node2 head;
void insertBegin(int data){
Node2 temp = new Node2(data);
temp.next = head;
head = temp;
}
void printList(){
Node2 curr = head;
while (curr != null){
System.out.print(curr.data+" ");
curr = curr.next;
}
System.out.println("");
}
void insertEnd(int data){
Node2 temp = new Node2(data);
Node2 curr = head;
if (head == null){
head = temp;
return;
}
while (curr.next != null){
curr=curr.next;
}
curr.next = temp;
}
}
public class NthPointerFromENd {
static void printNthNode(Node2 head,int n){
if (head == null)
return;
Node2 first = head,second = head;
for (int i = 0; i < n; i++) {
if (first == null)
return;
first = first.next;
}
while (first!=null){
second = second.next;
first = first.next;
}
System.out.println(second.data);
}
public static void main(String[] args) {
List2 list = new List2();
list.insertEnd(10);
list.insertEnd(20);
list.insertEnd(30);
list.insertEnd(40);
list.insertEnd(50);
list.insertEnd(60);
list.insertEnd(70);
printNthNode(list.head,3);
}
}