Skip to content

Commit 2580c0a

Browse files
committed
added approach 4 of 6 in java
1 parent d80da42 commit 2580c0a

File tree

2 files changed

+148
-1
lines changed

2 files changed

+148
-1
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import java.util.*;
2+
abstract class Approach4
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+
private int size; //size of the linked list
55+
private int[] hash; //hashtable for the linked list
56+
57+
//constructor for linked list
58+
public LinkedList()
59+
{
60+
head=null;
61+
size=0;
62+
hash = new int[10];
63+
}
64+
65+
/**
66+
* insert_at_head()
67+
* it will insert node at the head of the linked list
68+
* @param data
69+
**/
70+
public void insert_at_head(int data)
71+
{
72+
Node newNode = new Node(data);
73+
if(head==null)
74+
{
75+
head=newNode;
76+
hash[size]=newNode.getData();
77+
}
78+
else
79+
{
80+
newNode.setNext(head);
81+
head=newNode;
82+
hash[size]=newNode.getData();
83+
}
84+
size++;
85+
}
86+
87+
/**
88+
* print_list()
89+
* it will print the linked list
90+
**/
91+
public void printList()
92+
{
93+
if(head==null)
94+
{
95+
System.out.println("NULL");
96+
}
97+
else
98+
{
99+
Node current=head;
100+
while(current!=null)
101+
{
102+
System.out.print(current.getData() +" -> ");
103+
current=current.getNext();
104+
}
105+
System.out.println("NULL");
106+
}
107+
}
108+
109+
/*====================================================================*/
110+
/**
111+
* APPROACH 4
112+
* Using hashtables while adding elements in linked list
113+
* Then we get the size
114+
* find index of hashtable by subtracting the nthnode from size
115+
* print node at index from hashtable
116+
**/
117+
public void printNthNodeFromLast(int num)
118+
{
119+
if(num>size)
120+
{
121+
System.out.println("INDEX OUT OF BOUNDS");
122+
return;
123+
}
124+
System.out.println(hash[num-1]);
125+
}
126+
/*====================================================================*/
127+
}
128+
129+
public static void main(String[] args)
130+
{
131+
LinkedList ll = new LinkedList();
132+
ll.insert_at_head(3);
133+
ll.insert_at_head(1);
134+
ll.insert_at_head(4);
135+
ll.insert_at_head(2);
136+
ll.insert_at_head(5);
137+
138+
ll.printList();
139+
140+
Scanner in = new Scanner(System.in);
141+
System.out.println("Enter the nth node from last");
142+
int n = in.nextInt();
143+
144+
ll.printNthNodeFromLast(n);
145+
in.close();
146+
}
147+
}

Java/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* APPROACH 1: [Compute the size while adding](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach1.java)
4444
* APPROACH 2: [Using two current pointers](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach2.java)
4545
* APPROACH 3: [Using hashtable](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach3.java)
46-
* APPROACH 4: Using Hashtable while adding
46+
* APPROACH 4: [Using Hashtable while adding](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach4.java)
4747
* APPROACH 5: Finding node in one scan
4848
* APPROACH 6: Using recursion
4949

0 commit comments

Comments
 (0)