-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from HarshitPachori/dev
feat: added Nth Node From Linkedlist End Program -> Java
- Loading branch information
Showing
3 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
Beginner Level π/Nth_Node_From_End_LinkedList/Question.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Problem Statement | ||
|
||
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`. | ||
|
||
## Input | ||
|
||
- The linked list is constructed from `Node` instances, each containing: | ||
- `data` (integer): The value of the node. | ||
- `next` (Node): The next node in the list or `null` if itβs the last node. | ||
- `n` (integer): The position from the end of the list to find. | ||
|
||
## Output | ||
|
||
- An integer representing the data of the nth node from the end. | ||
- Return `-1` if `n` is larger than the length of the list. | ||
|
||
## Constraints | ||
|
||
1. `1 <= n <= 10^5` | ||
2. The linked list will contain only non-negative integers. | ||
3. The linked list length will be between `1` and `10^5`. | ||
|
||
## Example | ||
|
||
### Example 1 | ||
|
||
**Input:** | ||
|
||
- Linked List: `0 -> 1 -> 2 -> 3 -> 4 -> 5 -> Null` | ||
- `n = 2` | ||
|
||
**Output:** `4` | ||
|
||
**Explanation:** The second node from the end of the list has the value `4`. | ||
|
||
### Example 2 | ||
|
||
**Input:** | ||
|
||
- Linked List: `1 -> 2 -> 3 -> Null` | ||
- `n = 4` | ||
|
||
**Output:** `-1` | ||
|
||
**Explanation:** `n` is greater than the list length, so the function returns `-1`. | ||
|
||
### Time Complexity | ||
|
||
- **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. | ||
|
||
### Space Complexity | ||
|
||
- **O(1)**, as no extra space is used apart from a few pointers for traversal. |
57 changes: 57 additions & 0 deletions
57
Beginner Level π/Nth_Node_From_End_LinkedList/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
public class Solution { | ||
Node head; | ||
|
||
static class Node { | ||
int data; | ||
Node next; | ||
|
||
Node(int d) { | ||
this.data = d; | ||
this.next = null; | ||
} | ||
} | ||
|
||
int nthNodeFromEnd(Node head, int n) { | ||
Node temp = head; | ||
int len = 0; | ||
while (temp != null) { | ||
len++; | ||
temp = temp.next; | ||
} | ||
temp = head; | ||
if (len < n) | ||
return -1; | ||
for (int i = 1; i < len - n + 1; i++) { | ||
temp = temp.next; | ||
} | ||
return temp.data; | ||
|
||
} | ||
|
||
void printList() { | ||
Node temp = head; | ||
while (temp != null) { | ||
System.out.print(temp.data + " -> "); | ||
temp = temp.next; | ||
} | ||
System.out.print("Null"); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Solution nnfl = new Solution(); | ||
nnfl.head = new Node(0); | ||
|
||
Node a = new Node(1); | ||
Node b = new Node(2); | ||
Node c = new Node(3); | ||
Node d = new Node(4); | ||
Node e = new Node(5); | ||
nnfl.head.next = a; | ||
a.next = b; | ||
b.next = c; | ||
c.next = d; | ||
d.next = e; | ||
System.out.println(nnfl.nthNodeFromEnd(a, 2)); | ||
nnfl.printList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters