|
| 1 | +import java.util.*; |
| 2 | +abstract class Approach2 |
| 3 | +{ |
| 4 | + /** |
| 5 | + * LINKED LIST CLASS |
| 6 | + **/ |
| 7 | + private static class LinkedList |
| 8 | + { |
| 9 | + /** |
| 10 | + * NODE CLASS |
| 11 | + * for linked list node |
| 12 | + **/ |
| 13 | + private class Node |
| 14 | + { |
| 15 | + private int data; |
| 16 | + private Node next; |
| 17 | + |
| 18 | + //constructor |
| 19 | + public Node(int data) |
| 20 | + { |
| 21 | + this.data=data; |
| 22 | + next=null; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * getData() |
| 27 | + * @return data in the node |
| 28 | + **/ |
| 29 | + public int getData() |
| 30 | + { |
| 31 | + return data; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * getNext() |
| 36 | + * @return the next node |
| 37 | + **/ |
| 38 | + public Node getNext() |
| 39 | + { |
| 40 | + return next; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * setNext() |
| 45 | + * @param next |
| 46 | + **/ |
| 47 | + public void setNext(Node next) |
| 48 | + { |
| 49 | + this.next=next; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private Node head; //head of the linked list |
| 54 | + |
| 55 | + //constructor for linked list |
| 56 | + public LinkedList() |
| 57 | + { |
| 58 | + head=null; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * insert_at_head() |
| 63 | + * it will insert node at the head of the linked list |
| 64 | + * @param data |
| 65 | + **/ |
| 66 | + public void insert_at_head(int data) |
| 67 | + { |
| 68 | + Node newNode = new Node(data); |
| 69 | + if(head==null) |
| 70 | + { |
| 71 | + head=newNode; |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + newNode.setNext(head); |
| 76 | + head=newNode; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * print_list() |
| 82 | + * it will print the linked list |
| 83 | + **/ |
| 84 | + public void printList() |
| 85 | + { |
| 86 | + if(head==null) |
| 87 | + { |
| 88 | + System.out.println("NULL"); |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + Node current=head; |
| 93 | + while(current!=null) |
| 94 | + { |
| 95 | + System.out.print(current.getData() +" -> "); |
| 96 | + current=current.getNext(); |
| 97 | + } |
| 98 | + System.out.println("NULL"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /*====================================================================*/ |
| 103 | + /** |
| 104 | + * APPROACH 2 |
| 105 | + * Using two current pointers |
| 106 | + * Find the remaining nodes from right on every node |
| 107 | + * Find the count of remaining nodes |
| 108 | + * if count is equal to the nth node to be founded then print that node |
| 109 | + * if count is less than the nth node return |
| 110 | + * if count is grater than the nth node then move current forward |
| 111 | + **/ |
| 112 | + public void printNthNodeFromLast(int num) |
| 113 | + { |
| 114 | + Node current=head; |
| 115 | + while(current!=null) |
| 116 | + { |
| 117 | + int count=0; //count of remaining node |
| 118 | + Node current2=current; |
| 119 | + while(current2!=null) |
| 120 | + { |
| 121 | + count++; |
| 122 | + current2=current2.getNext(); |
| 123 | + } |
| 124 | + |
| 125 | + if(count==num) |
| 126 | + { |
| 127 | + System.out.println(current.getData()); |
| 128 | + return; |
| 129 | + } |
| 130 | + else if(count<num) |
| 131 | + { |
| 132 | + System.out.println("fewer number of nodes"); |
| 133 | + return; |
| 134 | + } |
| 135 | + else if(count>num) |
| 136 | + { |
| 137 | + current=current.getNext(); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + /*====================================================================*/ |
| 142 | + } |
| 143 | + |
| 144 | + public static void main(String[] args) |
| 145 | + { |
| 146 | + LinkedList ll = new LinkedList(); |
| 147 | + ll.insert_at_head(3); |
| 148 | + ll.insert_at_head(1); |
| 149 | + ll.insert_at_head(4); |
| 150 | + ll.insert_at_head(2); |
| 151 | + ll.insert_at_head(5); |
| 152 | + |
| 153 | + ll.printList(); |
| 154 | + |
| 155 | + Scanner in = new Scanner(System.in); |
| 156 | + System.out.println("Enter the nth node from last"); |
| 157 | + int n = in.nextInt(); |
| 158 | + |
| 159 | + ll.printNthNodeFromLast(n); |
| 160 | + in.close(); |
| 161 | + } |
| 162 | +} |
0 commit comments