From 0ba93c8d1738bcdb869c066e8bc62d645d0275f9 Mon Sep 17 00:00:00 2001 From: Suraj Kumar Modi <76468931+skmodi649@users.noreply.github.com> Date: Mon, 29 Nov 2021 18:06:38 +0530 Subject: [PATCH 1/5] Add algorithms_random_node.java --- .../lists/Algorithms_random_node.java | 94 +++++++++++++++++++ .../datastructures/lists/README.md | 3 +- 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/thealgorithms/datastructures/lists/Algorithms_random_node.java diff --git a/src/main/java/com/thealgorithms/datastructures/lists/Algorithms_random_node.java b/src/main/java/com/thealgorithms/datastructures/lists/Algorithms_random_node.java new file mode 100644 index 000000000000..ccebf716f83d --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/lists/Algorithms_random_node.java @@ -0,0 +1,94 @@ +/** Author : Suraj Kumar + * Github : https://github.com/skmodi649 + */ + +/** PROBLEM DESCRIPTION : + * There is a single linked list and we are supposed to find a random node in the given linked list + */ + +/** ALGORITHM : + * Step 1 : START + * Step 2 : Create an arraylist of type integer + * Step 3 : Declare an integer type variable for size and linked list type for head + * Step 4 : We will use two methods, one for traversing through the linked list using while loop and also increase the size by 1 + * + * (a) Algorithms_random_node(head) + * (b) run a while loop till null; + * (c) add the value to arraylist; + * (d) increase the size; + * + * Step 5 : Now use another method for getting random values using Math.random() and return the value present in arraylist for the calculated index + * 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 + * Step 7 : STOP + */ + + +package com.thealgorithms.datastructures.lists; + + +import java.util.ArrayList; + +public class Algorithms_random_node { + + ArrayList list; + int size; + static ListNode head; + + + static class ListNode{ + int val; + ListNode next; + + ListNode(int val){ + this.val = val; + } + } + public Algorithms_random_node(ListNode head) { + list = new ArrayList<>(); + size = 0; + + ListNode temp = head; + + //Now using while loop to traverse through the linked list and + //go on adding values and increasing the size value by 1 + + while (temp != null) { + list.add(temp.val); + temp=temp.next; + size++; + } + } + + public int getRandom() { + int index = (int)(Math.random()*size); + return list.get(index); + } + + // Driver program to test above functions + public static void main(String[] args) { + + + head = new ListNode(15); + head.next = new ListNode(25); + head.next.next = new ListNode(4); + head.next.next.next = new ListNode(1); + head.next.next.next.next = new ListNode(78); + head.next.next.next.next.next = new ListNode(63); + Algorithms_random_node list = new Algorithms_random_node(head); + + int random_num = list.getRandom(); + System.out.println("Random Node : "+random_num); + } +} + + +/** OUTPUT : + * First output : + * Random Node : 25 + * Second output : + * Random Node : 78 + */ + +/** Time Complexity : O(n) + * Auxiliary Space Complexity : O(1) + */ diff --git a/src/main/java/com/thealgorithms/datastructures/lists/README.md b/src/main/java/com/thealgorithms/datastructures/lists/README.md index 813e1a5f84c0..774f4ee9a775 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/README.md +++ b/src/main/java/com/thealgorithms/datastructures/lists/README.md @@ -27,4 +27,5 @@ The `next` variable points to the next node in the data structure and value stor 3. `CountSinglyLinkedListRecursion.java`: Recursively counts the size of a list. 4. `CreateAndDetectLoop.java` : Create and detect a loop in a linked list. 5. `DoublyLinkedList.java` : A modification of singly linked list which has a `prev` pointer to point to the previous node. -6. `Merge_K_SortedLinkedlist.java` : Merges K sorted linked list with mergesort (mergesort is also the most efficient sorting algorithm for linked list). \ No newline at end of file +6. `Merge_K_SortedLinkedlist.java` : Merges K sorted linked list with mergesort (mergesort is also the most efficient sorting algorithm for linked list). +7. `Algorithms_random_node.java` : Selects a random node from given linked list and diplays it \ No newline at end of file From 5038330aa9b7536c545ab295db58f02bfee3f1c6 Mon Sep 17 00:00:00 2001 From: Suraj Kumar Modi <76468931+skmodi649@users.noreply.github.com> Date: Mon, 29 Nov 2021 18:27:42 +0530 Subject: [PATCH 2/5] Update Algorithms_random_node.java --- .../lists/Algorithms_random_node.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/Algorithms_random_node.java b/src/main/java/com/thealgorithms/datastructures/lists/Algorithms_random_node.java index ccebf716f83d..980a9c300d59 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/Algorithms_random_node.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/Algorithms_random_node.java @@ -4,7 +4,7 @@ /** PROBLEM DESCRIPTION : * There is a single linked list and we are supposed to find a random node in the given linked list - */ + */ /** ALGORITHM : * Step 1 : START @@ -30,21 +30,21 @@ public class Algorithms_random_node { - ArrayList list; + ArrayList < Integer > list; int size; static ListNode head; - static class ListNode{ + static class ListNode { int val; ListNode next; - ListNode(int val){ + ListNode(int val) { this.val = val; } } public Algorithms_random_node(ListNode head) { - list = new ArrayList<>(); + list = new ArrayList < > (); size = 0; ListNode temp = head; @@ -54,13 +54,13 @@ public Algorithms_random_node(ListNode head) { while (temp != null) { list.add(temp.val); - temp=temp.next; + temp = temp.next; size++; } } public int getRandom() { - int index = (int)(Math.random()*size); + int index = (int)(Math.random() * size); return list.get(index); } @@ -77,7 +77,7 @@ public static void main(String[] args) { Algorithms_random_node list = new Algorithms_random_node(head); int random_num = list.getRandom(); - System.out.println("Random Node : "+random_num); + System.out.println("Random Node : " + random_num); } } @@ -91,4 +91,4 @@ public static void main(String[] args) { /** Time Complexity : O(n) * Auxiliary Space Complexity : O(1) - */ + */ \ No newline at end of file From ce88cb3843d02c83732fa35d3a6f6c7c8cfbb675 Mon Sep 17 00:00:00 2001 From: Suraj Kumar Modi <76468931+skmodi649@users.noreply.github.com> Date: Mon, 29 Nov 2021 23:21:16 +0530 Subject: [PATCH 3/5] All changes made --- .../thealgorithms/datastructures/lists/README.md | 2 +- ...gorithms_random_node.java => RandomNode.java} | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) rename src/main/java/com/thealgorithms/datastructures/lists/{Algorithms_random_node.java => RandomNode.java} (85%) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/README.md b/src/main/java/com/thealgorithms/datastructures/lists/README.md index 774f4ee9a775..7030c3f82f84 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/README.md +++ b/src/main/java/com/thealgorithms/datastructures/lists/README.md @@ -28,4 +28,4 @@ The `next` variable points to the next node in the data structure and value stor 4. `CreateAndDetectLoop.java` : Create and detect a loop in a linked list. 5. `DoublyLinkedList.java` : A modification of singly linked list which has a `prev` pointer to point to the previous node. 6. `Merge_K_SortedLinkedlist.java` : Merges K sorted linked list with mergesort (mergesort is also the most efficient sorting algorithm for linked list). -7. `Algorithms_random_node.java` : Selects a random node from given linked list and diplays it \ No newline at end of file +7. `RandomNode.java` : Selects a random node from given linked list and diplays it. \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/datastructures/lists/Algorithms_random_node.java b/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java similarity index 85% rename from src/main/java/com/thealgorithms/datastructures/lists/Algorithms_random_node.java rename to src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java index 980a9c300d59..d6755d11bf6c 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/Algorithms_random_node.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java @@ -12,7 +12,7 @@ * Step 3 : Declare an integer type variable for size and linked list type for head * Step 4 : We will use two methods, one for traversing through the linked list using while loop and also increase the size by 1 * - * (a) Algorithms_random_node(head) + * (a) RandomNode(head) * (b) run a while loop till null; * (c) add the value to arraylist; * (d) increase the size; @@ -28,9 +28,9 @@ import java.util.ArrayList; -public class Algorithms_random_node { +public class RandomNode { - ArrayList < Integer > list; + ArrayList list; int size; static ListNode head; @@ -43,8 +43,8 @@ static class ListNode { this.val = val; } } - public Algorithms_random_node(ListNode head) { - list = new ArrayList < > (); + public RandomNode(ListNode head) { + list = new ArrayList <>(); size = 0; ListNode temp = head; @@ -74,10 +74,10 @@ public static void main(String[] args) { head.next.next.next = new ListNode(1); head.next.next.next.next = new ListNode(78); head.next.next.next.next.next = new ListNode(63); - Algorithms_random_node list = new Algorithms_random_node(head); + RandomNode list = new RandomNode(head); - int random_num = list.getRandom(); - System.out.println("Random Node : " + random_num); + int randomNum = list.getRandom(); + System.out.println("Random Node : " + randomNum); } } From fa4118efb21a20e14ad5dc46cf09c8565bf393c5 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Tue, 30 Nov 2021 09:37:45 +0800 Subject: [PATCH 4/5] Update RandomNode.java --- .../datastructures/lists/RandomNode.java | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java b/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java index d6755d11bf6c..4e54489d20c8 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java @@ -27,13 +27,13 @@ import java.util.ArrayList; +import java.util.List; +import java.util.Random; public class RandomNode { - - ArrayList list; - int size; - static ListNode head; - + private List list; + private int size; + private static Random rand = new Random(); static class ListNode { int val; @@ -43,10 +43,9 @@ static class ListNode { this.val = val; } } - public RandomNode(ListNode head) { - list = new ArrayList <>(); - size = 0; + public RandomNode(ListNode head) { + list = new ArrayList<>(); ListNode temp = head; //Now using while loop to traverse through the linked list and @@ -60,35 +59,37 @@ public RandomNode(ListNode head) { } public int getRandom() { - int index = (int)(Math.random() * size); + int index = rand.nextInt(size); return list.get(index); } // Driver program to test above functions public static void main(String[] args) { - - - head = new ListNode(15); + ListNode head = new ListNode(15); head.next = new ListNode(25); head.next.next = new ListNode(4); head.next.next.next = new ListNode(1); head.next.next.next.next = new ListNode(78); head.next.next.next.next.next = new ListNode(63); RandomNode list = new RandomNode(head); - int randomNum = list.getRandom(); System.out.println("Random Node : " + randomNum); } } -/** OUTPUT : +/** + * OUTPUT : * First output : * Random Node : 25 * Second output : * Random Node : 78 + * Time Complexity : O(n) + * Auxiliary Space Complexity : O(1) + * Time Complexity : O(n) + * Auxiliary Space Complexity : O(1) */ /** Time Complexity : O(n) * Auxiliary Space Complexity : O(1) - */ \ No newline at end of file + */ From 0738657834d6cbdf9f42f75812162b69704c5684 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Tue, 30 Nov 2021 09:42:50 +0800 Subject: [PATCH 5/5] Update RandomNode.java --- .../com/thealgorithms/datastructures/lists/RandomNode.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java b/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java index 4e54489d20c8..72cacab4331f 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java @@ -25,7 +25,6 @@ package com.thealgorithms.datastructures.lists; - import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -48,9 +47,8 @@ public RandomNode(ListNode head) { list = new ArrayList<>(); ListNode temp = head; - //Now using while loop to traverse through the linked list and - //go on adding values and increasing the size value by 1 - + // Now using while loop to traverse through the linked list and + // go on adding values and increasing the size value by 1 while (temp != null) { list.add(temp.val); temp = temp.next;