Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e3723e8

Browse files
added approach 5 of 6
1 parent 2580c0a commit e3723e8

File tree

2 files changed

+166
-1
lines changed

2 files changed

+166
-1
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import java.util.*;
2+
abstract class Approach5
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+
size=0;
60+
hash = new int[1000];
61+
counter=0;
62+
}
63+
64+
/**
65+
* insert_at_head()
66+
* it will insert node at the head of the linked list
67+
* @param data
68+
**/
69+
public void insert_at_head(int data)
70+
{
71+
Node newNode = new Node(data);
72+
if(head==null)
73+
{
74+
head=newNode;
75+
}
76+
else
77+
{
78+
newNode.setNext(head);
79+
head=newNode;
80+
}
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+
/**
107+
* APPROACH 5
108+
* Finding in one scan of linked list
109+
* take current pointer set to head
110+
* traverse upto the nth node
111+
* take current2 set it to null
112+
* traverse current again upto null
113+
* move current2 also
114+
* when current is fully traversed current2 node will be printed
115+
**/
116+
public void printNthNodeFromLast(int num)
117+
{
118+
Node current=head;
119+
int i=0;
120+
while(current!=null) //first traversed half upto nth node
121+
{
122+
if(num-1==i)
123+
{
124+
break;
125+
}
126+
i++;
127+
current=current.getNext();
128+
}
129+
Node last=null;
130+
while(current!=null) //traversed upto end
131+
{
132+
if(last==null)
133+
{
134+
last=head;
135+
}
136+
else
137+
{
138+
last=last.getNext();
139+
}
140+
current=current.getNext();
141+
}
142+
System.out.println(last.getData());
143+
}
144+
/*====================================================================*/
145+
}
146+
147+
public static void main(String[] args)
148+
{
149+
LinkedList ll = new LinkedList();
150+
ll.insert_at_head(3);
151+
ll.insert_at_head(1);
152+
ll.insert_at_head(4);
153+
ll.insert_at_head(2);
154+
ll.insert_at_head(5);
155+
156+
ll.printList();
157+
158+
Scanner in = new Scanner(System.in);
159+
System.out.println("Enter the nth node from last");
160+
int n = in.nextInt();
161+
162+
ll.printNthNodeFromLast(n);
163+
in.close();
164+
}
165+
}

‎Java/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
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)
4646
* APPROACH 4: [Using Hashtable while adding](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach4.java)
47-
* APPROACH 5: Finding node in one scan
47+
* APPROACH 5: [Finding node in one scan](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach5.java)
4848
* APPROACH 6: Using recursion
4949

5050
#### STACKS

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /