From d80da42953fa7f8bcef5fdec74537531ec5d126d Mon Sep 17 00:00:00 2001 From: GauravWalia19 Date: Sat, 29 Dec 2018 00:57:11 +0530 Subject: [PATCH] added approach 3 of 6 in java --- .../MISC/Nth-node-from-end/Approach3.java | 150 ++++++++++++++++++ Java/README.md | 2 +- 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach3.java diff --git a/Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach3.java b/Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach3.java new file mode 100644 index 0000000..ed08ba1 --- /dev/null +++ b/Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach3.java @@ -0,0 +1,150 @@ +import java.util.*; +abstract class Approach3 +{ + /** + * LINKED LIST CLASS + **/ + private static class LinkedList + { + /** + * NODE CLASS + * for linked list node + **/ + private class Node + { + private int data; + private Node next; + + //constructor + public Node(int data) + { + this.data=data; + next=null; + } + + /** + * getData() + * @return data in the node + **/ + public int getData() + { + return data; + } + + /** + * getNext() + * @return the next node + **/ + public Node getNext() + { + return next; + } + + /** + * setNext() + * @param next + **/ + public void setNext(Node next) + { + this.next=next; + } + } + + private Node head; //head of the linked list + + //constructor for linked list + public LinkedList() + { + head=null; + } + + /** + * insert_at_head() + * it will insert node at the head of the linked list + * @param data + **/ + public void insert_at_head(int data) + { + Node newNode = new Node(data); + if(head==null) + { + head=newNode; + } + else + { + newNode.setNext(head); + head=newNode; + } + } + + /** + * print_list() + * it will print the linked list + **/ + public void printList() + { + if(head==null) + { + System.out.println("NULL"); + } + else + { + Node current=head; + while(current!=null) + { + System.out.print(current.getData() +" -> "); + current=current.getNext(); + } + System.out.println("NULL"); + } + } + + /*====================================================================*/ + /** + * APPROACH 3 + * Using hashtable + * traverse the list and make hashtable + * we will get the size of the list + * we will calculate nth node from hashtable by substracting the nthnode from size + **/ + public void printNthNodeFromLast(int num) + { + int[] hash = new int[10]; //hashtable + int size=0; + Node current=head; + + while(current!=null) //traversing and making hashtable + { + hash[size]=current.getData(); + current=current.getNext(); + size++; + } + if(num>size) + { + System.out.println("INDEX OUT OF BOUNDS"); + return; + } + System.out.println(hash[size-num]); + } + /*====================================================================*/ + } + + public static void main(String[] args) + { + LinkedList ll = new LinkedList(); + ll.insert_at_head(3); + ll.insert_at_head(1); + ll.insert_at_head(4); + ll.insert_at_head(2); + ll.insert_at_head(5); + + ll.printList(); + + Scanner in = new Scanner(System.in); + System.out.println("Enter the nth node from last"); + int n = in.nextInt(); + + ll.printNthNodeFromLast(n); + in.close(); + } +} diff --git a/Java/README.md b/Java/README.md index d59d023..b1d23e8 100644 --- a/Java/README.md +++ b/Java/README.md @@ -42,7 +42,7 @@ * Find the nth node from end in single linked list * APPROACH 1: [Compute the size while adding](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach1.java) * APPROACH 2: [Using two current pointers](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach2.java) - * APPROACH 3: Using hashtable + * APPROACH 3: [Using hashtable](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach3.java) * APPROACH 4: Using Hashtable while adding * APPROACH 5: Finding node in one scan * APPROACH 6: Using recursion