From 91a83097e7524425fcf81ba69c6c0ecdbe3c1c48 Mon Sep 17 00:00:00 2001 From: HarshitPachori Date: Wed, 30 Oct 2024 14:53:23 +0530 Subject: [PATCH 1/2] feat: added Nth Node From Linkedlist End Program -> Java --- .../Nth_Node_From_End_LinkedList/Question.md" | 53 +++++++++++++++++ .../Solution.java" | 57 +++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 "Beginner Level \360\237\223\201/Nth_Node_From_End_LinkedList/Question.md" create mode 100644 "Beginner Level \360\237\223\201/Nth_Node_From_End_LinkedList/Solution.java" diff --git "a/Beginner Level \360\237\223\201/Nth_Node_From_End_LinkedList/Question.md" "b/Beginner Level \360\237\223\201/Nth_Node_From_End_LinkedList/Question.md" new file mode 100644 index 0000000..872b75b --- /dev/null +++ "b/Beginner Level \360\237\223\201/Nth_Node_From_End_LinkedList/Question.md" @@ -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. diff --git "a/Beginner Level \360\237\223\201/Nth_Node_From_End_LinkedList/Solution.java" "b/Beginner Level \360\237\223\201/Nth_Node_From_End_LinkedList/Solution.java" new file mode 100644 index 0000000..3dfe7a3 --- /dev/null +++ "b/Beginner Level \360\237\223\201/Nth_Node_From_End_LinkedList/Solution.java" @@ -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(); + } +} \ No newline at end of file From 36cc45f7680ecabce48c089061834cd088040c53 Mon Sep 17 00:00:00 2001 From: Harshit Pachori <86182695+HarshitPachori@users.noreply.github.com> Date: Thu, 31 Oct 2024 07:04:40 +0530 Subject: [PATCH 2/2] Update data.json --- "Beginner Level \360\237\223\201/data.json" | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git "a/Beginner Level \360\237\223\201/data.json" "b/Beginner Level \360\237\223\201/data.json" index 4715abc..b32ff55 100644 --- "a/Beginner Level \360\237\223\201/data.json" +++ "b/Beginner Level \360\237\223\201/data.json" @@ -58,13 +58,17 @@ "name": "Nimra Asalm", "githubUsername":"NimraAslamKhan" }, - +{ "name": "Dhara Bindal", "githubUsername": "bindaldhara" - } + }, { "name": "Avid Coder", "githubUsername": "AvidCoder101" + }, + { + "name":"Harshit Pachori", + "githubUsername":"HarshitPachori" } ]