Skip to content

Commit 8546d47

Browse files
committed code related to NthNode from the End of Linked list
1 parent 9783c6f commit 8546d47

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed
Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
11
package com.java4u.ds.linkedlist.nthNode;
22

3+
import com.java4u.ds.linkedlist.Node;
4+
35
public class NthNodeFromLast {
6+
public Node NthNodeFromEnd(Node head, int n) {
7+
Node temp = head;
8+
Node pthNode = null;
9+
for (int i = 0; i < n; i++) {
10+
if (temp != null) {
11+
temp = temp.getNext();
12+
}
13+
}
14+
while (temp != null) {
15+
if (pthNode != null) {
16+
pthNode = head;
17+
} else {
18+
pthNode = pthNode.getNext();
19+
}
20+
temp = temp.getNext();
21+
}
422

5-
6-
public static void main(String[] args) {
7-
23+
if (pthNode != null) {
24+
return pthNode;
25+
} else {
26+
return null;
27+
}
828

929
}
1030

31+
public static void main(String[] args) {
32+
NthNodeFromLast list = new NthNodeFromLast();
33+
34+
Node head = new Node(1);
35+
Node n1 = new Node(2);
36+
Node n2 = new Node(3);
37+
Node n3 = new Node(4);
38+
Node n4 = new Node(5);
39+
Node n5 = new Node(6);
40+
head.setNext(n1);
41+
n1.setNext(n2);
42+
n2.setNext(n3);
43+
n3.setNext(n4);
44+
n4.setNext(n5);
45+
Node node = list.NthNodeFromEnd(head, 2);
46+
if (node != null) {
47+
System.out.println(node.getData());
48+
}
49+
50+
}
1151
}
Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,55 @@
11
package com.java4u.ds.linkedlist.nthNode;
22

3+
import com.java4u.ds.linkedlist.Node;
4+
35
public class NthNodeFromLastUsingRecursion {
46

5-
public static void main(String[] args) {
6-
// TODO Auto-generated method stub
7+
int count = 0;
8+
9+
public int nthNodeFromEnd(Node head, int nodeFromLast) {
10+
11+
if (nodeFromLast <= 0 || head == null) {
12+
return -1;
13+
}
14+
15+
Node ptrA = head, ptrB = head;
16+
17+
// Moving ptrB N nodes from start keeping ptrA at head node.
18+
for (int i = 0; i < nodeFromLast; i++) {
719

20+
// If ptrB reaches NULL, then return -1 as indication that given Nth value is
21+
// incorrect and list doesn't contain N elements.
22+
if (ptrB == null) {
23+
return -1;
24+
}
25+
ptrB = ptrB.getNext();
26+
}
27+
28+
// Now incrementing ptrA and ptrB one node at a time until ptrB encounters NULL,
29+
// when it encounters NULL then ptrA will be at Nth position from end of list.
30+
while (ptrB != null) {
31+
ptrB = ptrB.getNext();
32+
ptrA = ptrA.getNext();
33+
}
34+
35+
return ptrA.getData();
36+
37+
}
38+
39+
public static void main(String[] args) {
40+
NthNodeFromLastUsingRecursion list = new NthNodeFromLastUsingRecursion();
41+
Node head = new Node(1);
42+
Node n1 = new Node(2);
43+
Node n2 = new Node(3);
44+
Node n3 = new Node(4);
45+
Node n4 = new Node(5);
46+
Node n5 = new Node(6);
47+
head.setNext(n1);
48+
n1.setNext(n2);
49+
n2.setNext(n3);
50+
n3.setNext(n4);
51+
n4.setNext(n5);
52+
System.out.println(list.nthNodeFromEnd(head, 2));
853
}
954

1055
}

0 commit comments

Comments
 (0)