java ( 2180278 ) - java ( 2180301 ) stack: com.thealgorithms.datastructures.lists.RandomNode.main(RandomNode.java:78)
package com.thealgorithms.datastructures.lists;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
 * @author Suraj Kumar
 * 
 * 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
 */
public class RandomNode {
    private final List list;
    private int size;
    private static final 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);
    }
    /**
     * 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)
     */
    // 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);
    }
} 
 | No. | From | Name | Value | 
|---|---|---|---|
| 1 | class@78 | RAND | java.util.Random@4aa298b7 | 
| 2 | 78 | args | [Ljava.lang.String;@74a14482 | 
| END | 0 | 0 | 0 | 
Functions and Shortcuts
| No. | Function | Shortcuts | Description | 
|---|---|---|---|
| 1 | GB | Alt + LEFT, Alt + A | Go Backward | 
| 2 | GF | Alt + RIGHT, Alt + D | Go Foreward | 
| 3 | PPE | Alt + UP, Alt + W | Previous Process End | 
| 4 | NPS | Alt + DOWN, Alt + S | Next Process Start | 
| 5 | PB | Ctrl + LEFT, Ctrl + A | current Process Backward | 
| 6 | PF | Ctrl + RIGHT, Ctrl + D | current Process Foreward | 
| 7 | PPTE | Ctrl + UP, Ctrl + W | go to current Process's Previous Thread's End | 
| 8 | PNTS | Ctrl + DOWN, Ctrl + S | go to current Process's Next Thread's Start | 
| 9 | TB | LEFT, A | current Thread Backward | 
| 10 | TF | RIGHT, D | current Thread Foreward | 
| 11 | LU | UP, W | go Line Up of current code block in current thread | 
| 12 | LD | DOWN, S | go Line Down of current code block in current thread | 
| 13 | LP | Shift + UP, Shift + W | go to the occurrence of current line in Previous Loop | 
| 14 | LD | Shift + DOWN, Shift + S | go to the occurrence of current line in Next Loop | 
| 15 | BS | Home | go to code Block Start | 
| 16 | BE | End | go to code Block End | 
| Project: | Alg-Java | 
| Update: | 20240824 | 
| Commit: | a7cd97d7 | 
| Source Code: | datastructures.lists.RandomNode | 
| BuildTool: | Java17 | 
| Compiler: | Java17 | 
| Runtime: | Openjdk17 | 
| System: | MySystemD | 
| Kernel: | Linux5.10.211 | 
| Cpu: | Intel:Corei7-7700K | 
| Machine: | AwesomeMachine |