From 6b0cdca8148349edc60189d28a57f85f322e1555 Mon Sep 17 00:00:00 2001 From: GauravWalia19 Date: Sat, 29 Dec 2018 01:23:54 +0530 Subject: [PATCH] added approach 6 of 6 in java --- .../MISC/Nth-node-from-end/Approach5.java | 3 - .../MISC/Nth-node-from-end/Approach6.java | 162 ++++++++++++++++++ Java/README.md | 2 +- 3 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach6.java diff --git a/Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach5.java b/Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach5.java index b994497..1badb26 100644 --- a/Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach5.java +++ b/Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach5.java @@ -56,9 +56,6 @@ public void setNext(Node next) public LinkedList() { head=null; - size=0; - hash = new int[1000]; - counter=0; } /** diff --git a/Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach6.java b/Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach6.java new file mode 100644 index 0000000..e643bda --- /dev/null +++ b/Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach6.java @@ -0,0 +1,162 @@ +import java.util.*; +abstract class Approach6 +{ + /** + * 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 + private int size; //size of the linked list + private int counter; + + //constructor for linked list + public LinkedList() + { + head=null; + size=0; + counter=0; + } + + /** + * getHead() + * @return the head node + **/ + public Node getHead() + { + return head; + } + + /** + * 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; + } + size++; + } + + /** + * 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 6 + * using recursion + * in backtracking count the nodes + * count equal to nthnode will be printed + **/ + public void printNthNodeFromLast(Node current,int num) + { + if(num>size) + { + System.out.println("OUT OF BOUND"); + return; + } + if(current==null) + { + return; + } + printNthNodeFromLast(current.getNext(),num); + counter++; + if(counter==num) + { + System.out.println(current.getData()); + return; + } + } + /*====================================================================*/ + } + + 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(ll.getHead(), n); + in.close(); + } +} diff --git a/Java/README.md b/Java/README.md index 95732cf..2b7c420 100644 --- a/Java/README.md +++ b/Java/README.md @@ -45,7 +45,7 @@ * APPROACH 3: [Using hashtable](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach3.java) * APPROACH 4: [Using Hashtable while adding](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach4.java) * APPROACH 5: [Finding node in one scan](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach5.java) - * APPROACH 6: Using recursion + * APPROACH 6: [Using recursion](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach6.java) #### STACKS