Skip to content

Commit bd50c36

Browse files
solves get node value
1 parent e996eae commit bd50c36

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
import java.util.Scanner;
2-
3-
public class GetNodeFromTail {
4-
public static void main(String[] args) {
5-
Scanner s = new Scanner(System.in);
6-
Node head = new Node();
7-
head.input(1);
8-
int position = s.nextInt();
9-
Node ans = nodeFromTail(head, position);
10-
System.out.println(ans.data);
11-
}
12-
13-
private static Node nodeFromTail(Node head, int position){
14-
int length = head.size();
15-
Node temp = head;
16-
for(int i=0 ; i<length-position-1 ; i++, temp = temp.next);
17-
return temp;
18-
}
19-
}
1+
package linkedlist;
2+
3+
import java.util.Scanner;
4+
5+
public class GetNodeValue {
6+
public static void main(String[] args) {
7+
Scanner s = new Scanner(System.in);
8+
Node head = new Node();
9+
head.input(1);
10+
int position = s.nextInt();
11+
Node ans = nodeFromTail(head, position);
12+
System.out.println(ans.data);
13+
}
14+
15+
private static Node nodeFromTail(Node head, int position){
16+
int length = head.size();
17+
Node temp = head;
18+
for(int i=0 ; i<length-position-1 ; i++, temp = temp.next);
19+
return temp;
20+
}
21+
}

0 commit comments

Comments
 (0)