Skip to content

Commit d11ff70

Browse files
committed
added approach 1 of 6 in java
1 parent 59c30da commit d11ff70

File tree

2 files changed

+160
-1
lines changed

2 files changed

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

Java/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@
3939
* [ARRAYLIST](Data-Structures/LISTS/INBUILT/arrayList.java)
4040
* [VECTORS](Data-Structures/LISTS/INBUILT/vectors.java)
4141
* MISC
42-
* Find the nth node from the end
42+
* Find the nth node from end in single linked list
43+
* APPROACH 1: [Compute the size while adding](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach1.java)
44+
* APPROACH 2: Using two current pointers
45+
* APPROACH 3: Using hashtable
46+
* APPROACH 4: Using Hashtable while adding
47+
* APPROACH 5: Finding node in one scan
48+
* APPROACH 6: Using recursion
4349

4450
#### STACKS
4551

0 commit comments

Comments
 (0)