Skip to content

Commit bc6de37

Browse files
skmodi649yanglbme
andauthored
Add RandomNode in lists section (TheAlgorithms#2851)
Co-authored-by: Yang Libin <[email protected]>
1 parent b870de4 commit bc6de37

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

src/main/java/com/thealgorithms/datastructures/lists/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ The `next` variable points to the next node in the data structure and value stor
2727
3. `CountSinglyLinkedListRecursion.java`: Recursively counts the size of a list.
2828
4. `CreateAndDetectLoop.java` : Create and detect a loop in a linked list.
2929
5. `DoublyLinkedList.java` : A modification of singly linked list which has a `prev` pointer to point to the previous node.
30-
6. `Merge_K_SortedLinkedlist.java` : Merges K sorted linked list with mergesort (mergesort is also the most efficient sorting algorithm for linked list).
30+
6. `Merge_K_SortedLinkedlist.java` : Merges K sorted linked list with mergesort (mergesort is also the most efficient sorting algorithm for linked list).
31+
7. `RandomNode.java` : Selects a random node from given linked list and diplays it.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/** Author : Suraj Kumar
2+
* Github : https://github.com/skmodi649
3+
*/
4+
5+
/** PROBLEM DESCRIPTION :
6+
* There is a single linked list and we are supposed to find a random node in the given linked list
7+
*/
8+
9+
/** ALGORITHM :
10+
* Step 1 : START
11+
* Step 2 : Create an arraylist of type integer
12+
* Step 3 : Declare an integer type variable for size and linked list type for head
13+
* Step 4 : We will use two methods, one for traversing through the linked list using while loop and also increase the size by 1
14+
*
15+
* (a) RandomNode(head)
16+
* (b) run a while loop till null;
17+
* (c) add the value to arraylist;
18+
* (d) increase the size;
19+
*
20+
* Step 5 : Now use another method for getting random values using Math.random() and return the value present in arraylist for the calculated index
21+
* Step 6 : Now in main() method we will simply insert nodes in the linked list and then call the appropriate method and then print the random node generated
22+
* Step 7 : STOP
23+
*/
24+
25+
26+
package com.thealgorithms.datastructures.lists;
27+
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
import java.util.Random;
31+
32+
public class RandomNode {
33+
private List<Integer> list;
34+
private int size;
35+
private static Random rand = new Random();
36+
37+
static class ListNode {
38+
int val;
39+
ListNode next;
40+
41+
ListNode(int val) {
42+
this.val = val;
43+
}
44+
}
45+
46+
public RandomNode(ListNode head) {
47+
list = new ArrayList<>();
48+
ListNode temp = head;
49+
50+
// Now using while loop to traverse through the linked list and
51+
// go on adding values and increasing the size value by 1
52+
while (temp != null) {
53+
list.add(temp.val);
54+
temp = temp.next;
55+
size++;
56+
}
57+
}
58+
59+
public int getRandom() {
60+
int index = rand.nextInt(size);
61+
return list.get(index);
62+
}
63+
64+
// Driver program to test above functions
65+
public static void main(String[] args) {
66+
ListNode head = new ListNode(15);
67+
head.next = new ListNode(25);
68+
head.next.next = new ListNode(4);
69+
head.next.next.next = new ListNode(1);
70+
head.next.next.next.next = new ListNode(78);
71+
head.next.next.next.next.next = new ListNode(63);
72+
RandomNode list = new RandomNode(head);
73+
int randomNum = list.getRandom();
74+
System.out.println("Random Node : " + randomNum);
75+
}
76+
}
77+
78+
79+
/**
80+
* OUTPUT :
81+
* First output :
82+
* Random Node : 25
83+
* Second output :
84+
* Random Node : 78
85+
* Time Complexity : O(n)
86+
* Auxiliary Space Complexity : O(1)
87+
* Time Complexity : O(n)
88+
* Auxiliary Space Complexity : O(1)
89+
*/
90+
91+
/** Time Complexity : O(n)
92+
* Auxiliary Space Complexity : O(1)
93+
*/

0 commit comments

Comments
 (0)