Movatterモバイル変換


[0]ホーム

URL:


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

Commitbc6de37

Browse files
Add RandomNode in lists section (TheAlgorithms#2851)
Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parentb870de4 commitbc6de37

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

Lines changed: 2 additions & 1 deletion
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.
Lines changed: 93 additions & 0 deletions
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+
packagecom.thealgorithms.datastructures.lists;
27+
28+
importjava.util.ArrayList;
29+
importjava.util.List;
30+
importjava.util.Random;
31+
32+
publicclassRandomNode {
33+
privateList<Integer>list;
34+
privateintsize;
35+
privatestaticRandomrand =newRandom();
36+
37+
staticclassListNode {
38+
intval;
39+
ListNodenext;
40+
41+
ListNode(intval) {
42+
this.val =val;
43+
}
44+
}
45+
46+
publicRandomNode(ListNodehead) {
47+
list =newArrayList<>();
48+
ListNodetemp =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+
publicintgetRandom() {
60+
intindex =rand.nextInt(size);
61+
returnlist.get(index);
62+
}
63+
64+
// Driver program to test above functions
65+
publicstaticvoidmain(String[]args) {
66+
ListNodehead =newListNode(15);
67+
head.next =newListNode(25);
68+
head.next.next =newListNode(4);
69+
head.next.next.next =newListNode(1);
70+
head.next.next.next.next =newListNode(78);
71+
head.next.next.next.next.next =newListNode(63);
72+
RandomNodelist =newRandomNode(head);
73+
intrandomNum =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)

[8]ページ先頭

©2009-2025 Movatter.jp