From e3723e8953ec9eccbda4a55544c80f580632059f Mon Sep 17 00:00:00 2001 From: GauravWalia19 Date: Sat, 29 Dec 2018 01:15:56 +0530 Subject: [PATCH] added approach 5 of 6 --- .../MISC/Nth-node-from-end/Approach5.java | 165 ++++++++++++++++++ Java/README.md | 2 +- 2 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach5.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 new file mode 100644 index 0000000..b994497 --- /dev/null +++ b/Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach5.java @@ -0,0 +1,165 @@ +import java.util.*; +abstract class Approach5 +{ + /** + * 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; + size=0; + hash = new int[1000]; + counter=0; + } + + /** + * 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 5 + * Finding in one scan of linked list + * take current pointer set to head + * traverse upto the nth node + * take current2 set it to null + * traverse current again upto null + * move current2 also + * when current is fully traversed current2 node will be printed + **/ + public void printNthNodeFromLast(int num) + { + Node current=head; + int i=0; + while(current!=null) //first traversed half upto nth node + { + if(num-1==i) + { + break; + } + i++; + current=current.getNext(); + } + Node last=null; + while(current!=null) //traversed upto end + { + if(last==null) + { + last=head; + } + else + { + last=last.getNext(); + } + current=current.getNext(); + } + System.out.println(last.getData()); + } + /*====================================================================*/ + } + + 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 95c8134..95732cf 100644 --- a/Java/README.md +++ b/Java/README.md @@ -44,7 +44,7 @@ * APPROACH 2: [Using two current pointers](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach2.java) * 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 + * APPROACH 5: [Finding node in one scan](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach5.java) * APPROACH 6: Using recursion #### STACKS