1
- /**
2
- * Author : Suraj Kumar
3
- * Github : https://github.com/skmodi649
4
- */
1
+ package com .thealgorithms .datastructures .lists ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+ import java .util .Random ;
5
6
6
7
/**
8
+ * @author <a href="https://github.com/skmodi649">Suraj Kumar</a>
9
+ * <p>
7
10
* PROBLEM DESCRIPTION :
8
11
* There is a single linked list and we are supposed to find a random node in the given linked list
9
- */
10
-
11
- /**
12
+ * <p>
12
13
* ALGORITHM :
13
14
* Step 1 : START
14
15
* Step 2 : Create an arraylist of type integer
15
16
* Step 3 : Declare an integer type variable for size and linked list type for head
16
17
* Step 4 : We will use two methods, one for traversing through the linked list using while loop and
17
18
* also increase the size by 1
18
- *
19
+ * <p>
19
20
* (a) RandomNode(head)
20
21
* (b) run a while loop till null;
21
22
* (c) add the value to arraylist;
22
23
* (d) increase the size;
23
- *
24
+ * <p>
24
25
* Step 5 : Now use another method for getting random values using Math.random() and return the
25
26
* value present in arraylist for the calculated index Step 6 : Now in main() method we will simply
26
27
* insert nodes in the linked list and then call the appropriate method and then print the random
27
28
* node generated Step 7 : STOP
28
29
*/
29
-
30
- package com .thealgorithms .datastructures .lists ;
31
-
32
- import java .util .ArrayList ;
33
- import java .util .List ;
34
- import java .util .Random ;
35
-
36
30
public class RandomNode {
37
31
38
- private List <Integer > list ;
32
+ private final List <Integer > list ;
39
33
private int size ;
40
- private static Random rand = new Random ();
34
+ private static final Random RAND = new Random ();
41
35
42
36
static class ListNode {
43
37
@@ -63,10 +57,23 @@ public RandomNode(ListNode head) {
63
57
}
64
58
65
59
public int getRandom () {
66
- int index = rand .nextInt (size );
60
+ int index = RAND .nextInt (size );
67
61
return list .get (index );
68
62
}
69
63
64
+ /**
65
+ * OUTPUT :
66
+ * First output :
67
+ * Random Node : 25
68
+ * Second output :
69
+ * Random Node : 78
70
+ * Time Complexity : O(n)
71
+ * Auxiliary Space Complexity : O(1)
72
+ * Time Complexity : O(n)
73
+ * Auxiliary Space Complexity : O(1)
74
+ * Time Complexity : O(n)
75
+ * Auxiliary Space Complexity : O(1)
76
+ */
70
77
// Driver program to test above functions
71
78
public static void main (String [] args ) {
72
79
ListNode head = new ListNode (15 );
@@ -80,18 +87,3 @@ public static void main(String[] args) {
80
87
System .out .println ("Random Node : " + randomNum );
81
88
}
82
89
}
83
- /**
84
- * OUTPUT :
85
- * First output :
86
- * Random Node : 25
87
- * Second output :
88
- * Random Node : 78
89
- * Time Complexity : O(n)
90
- * Auxiliary Space Complexity : O(1)
91
- * Time Complexity : O(n)
92
- * Auxiliary Space Complexity : O(1)
93
- */
94
- /**
95
- * Time Complexity : O(n)
96
- * Auxiliary Space Complexity : O(1)
97
- */
0 commit comments