-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnthNodefromEnd.java
98 lines (86 loc) · 1.99 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Given a LL and number n,
* print nth node from end of LL
*
* ip: 10 -> 20 -> 30 -> 40 -> 50, n = 2
* op: 40
*
* ip: 10 -> 20, n = 3
* op:
*
* ip: 10 -> 20 -> 30, n = 1
* op: 30
*
* Approach1:
* find len of LL
* ans: (len-n)th node
*
* Approach2: two ptrs
* we move firstptr n positions ahead from head
* we have secondptr from head
* move both the ptr in same speed
* when firstptr reaches null, secondptr will be present on op node
*
* Both of have time O(n)
*/
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
public class nthNodefromEnd {
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
// printNthFromEnd(head, 3);
printNthFromEnd_twoptr(head, 3);
}
public static void printNthFromEnd(Node head, int n) {
int len = 0;
Node curr = head;
while (curr != null) {
++len;
curr = curr.next;
}
if (len < n)
return;
curr = head;
int i = 0;
while (i < (len - n)) {
curr = curr.next;
++i;
}
System.out.println(curr.data);
}
public static void printNthFromEnd_twoptr(Node head, int n) {
if(head == null)
return;
Node first = head;
for(int i = 0; i < n; i++)
{
// if len<n
if (first == null)
return;
first = first.next;
}
Node second = head;
while(first != null)
{
second = second.next;
first = first.next;
}
System.out.println(second.data);
}
public static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
}
}