Skip to content

Commit c34a3a6

Browse files
Merge pull request #133 from HarshitPachori/dev
feat: added Nth Node From Linkedlist End Program -> Java
2 parents a6bd609 + 36cc45f commit c34a3a6

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Problem Statement
2+
3+
Given a singly linked list, write a function to find the nth node from the end of the list. The function should return the data of the nth node. If `n` is greater than the length of the list, return `-1`.
4+
5+
## Input
6+
7+
- The linked list is constructed from `Node` instances, each containing:
8+
- `data` (integer): The value of the node.
9+
- `next` (Node): The next node in the list or `null` if it’s the last node.
10+
- `n` (integer): The position from the end of the list to find.
11+
12+
## Output
13+
14+
- An integer representing the data of the nth node from the end.
15+
- Return `-1` if `n` is larger than the length of the list.
16+
17+
## Constraints
18+
19+
1. `1 <= n <= 10^5`
20+
2. The linked list will contain only non-negative integers.
21+
3. The linked list length will be between `1` and `10^5`.
22+
23+
## Example
24+
25+
### Example 1
26+
27+
**Input:**
28+
29+
- Linked List: `0 -> 1 -> 2 -> 3 -> 4 -> 5 -> Null`
30+
- `n = 2`
31+
32+
**Output:** `4`
33+
34+
**Explanation:** The second node from the end of the list has the value `4`.
35+
36+
### Example 2
37+
38+
**Input:**
39+
40+
- Linked List: `1 -> 2 -> 3 -> Null`
41+
- `n = 4`
42+
43+
**Output:** `-1`
44+
45+
**Explanation:** `n` is greater than the list length, so the function returns `-1`.
46+
47+
### Time Complexity
48+
49+
- **O(L)** where `L` is the length of the linked list. The function traverses the list twice, once to calculate the length and again to locate the nth node from the end.
50+
51+
### Space Complexity
52+
53+
- **O(1)**, as no extra space is used apart from a few pointers for traversal.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
public class Solution {
2+
Node head;
3+
4+
static class Node {
5+
int data;
6+
Node next;
7+
8+
Node(int d) {
9+
this.data = d;
10+
this.next = null;
11+
}
12+
}
13+
14+
int nthNodeFromEnd(Node head, int n) {
15+
Node temp = head;
16+
int len = 0;
17+
while (temp != null) {
18+
len++;
19+
temp = temp.next;
20+
}
21+
temp = head;
22+
if (len < n)
23+
return -1;
24+
for (int i = 1; i < len - n + 1; i++) {
25+
temp = temp.next;
26+
}
27+
return temp.data;
28+
29+
}
30+
31+
void printList() {
32+
Node temp = head;
33+
while (temp != null) {
34+
System.out.print(temp.data + " -> ");
35+
temp = temp.next;
36+
}
37+
System.out.print("Null");
38+
}
39+
40+
public static void main(String[] args) {
41+
Solution nnfl = new Solution();
42+
nnfl.head = new Node(0);
43+
44+
Node a = new Node(1);
45+
Node b = new Node(2);
46+
Node c = new Node(3);
47+
Node d = new Node(4);
48+
Node e = new Node(5);
49+
nnfl.head.next = a;
50+
a.next = b;
51+
b.next = c;
52+
c.next = d;
53+
d.next = e;
54+
System.out.println(nnfl.nthNodeFromEnd(a, 2));
55+
nnfl.printList();
56+
}
57+
}

β€ŽBeginner Level πŸ“/data.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@
5858
"name": "Nimra Asalm",
5959
"githubUsername":"NimraAslamKhan"
6060
},
61-
61+
{
6262
"name": "Dhara Bindal",
6363
"githubUsername": "bindaldhara"
6464

65-
}
65+
},
6666
{
6767
"name": "Avid Coder",
6868
"githubUsername": "AvidCoder101"
69+
},
70+
{
71+
"name":"Harshit Pachori",
72+
"githubUsername":"HarshitPachori"
6973
}
7074
]

0 commit comments

Comments
Β (0)