Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added Nth Node From Linkedlist End Program -> Java #133

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Beginner Level πŸ“/Nth_Node_From_End_LinkedList/Question.md
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 Beginner Level πŸ“/Nth_Node_From_End_LinkedList/Solution.java
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();
}
}
8 changes: 6 additions & 2 deletions Beginner Level πŸ“/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@
"name": "Nimra Asalm",
"githubUsername":"NimraAslamKhan"
},

{
"name": "Dhara Bindal",
"githubUsername": "bindaldhara"

}
},
{
"name": "Avid Coder",
"githubUsername": "AvidCoder101"
},
{
"name":"Harshit Pachori",
"githubUsername":"HarshitPachori"
}
]
Loading