From bc6de37a530209a931b85ae43dccf7c65c39d648 Mon Sep 17 00:00:00 2001 From: Suraj Kumar <76468931+skmodi649@users.noreply.github.com> Date: Tue, 30 Nov 2021 07:52:01 +0530 Subject: [PATCH] Add RandomNode in lists section (#2851) Co-authored-by: Yang Libin --- .../datastructures/lists/README.md | 3 +- .../datastructures/lists/RandomNode.java | 93 +++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java diff --git a/src/main/java/com/thealgorithms/datastructures/lists/README.md b/src/main/java/com/thealgorithms/datastructures/lists/README.md index 813e1a5f..7030c3f8 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. `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/RandomNode.java b/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java new file mode 100644 index 00000000..72cacab4 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java @@ -0,0 +1,93 @@ +/** 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) RandomNode(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; +import java.util.List; +import java.util.Random; + +public class RandomNode { + private List list; + private int size; + private static Random rand = new Random(); + + static class ListNode { + int val; + ListNode next; + + ListNode(int val) { + this.val = val; + } + } + + 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 + while (temp != null) { + list.add(temp.val); + temp = temp.next; + size++; + } + } + + public int getRandom() { + int index = rand.nextInt(size); + return list.get(index); + } + + // Driver program to test above functions + public static void main(String[] args) { + 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 : + * 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) + */