# Leetcode Linked List Random Node problem solution

In this Leetcode Linked List Random Node problem solution you have given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen.

Implement the Solution class:

1. Solution(ListNode head) Initializes the object with the integer array nums.
2. int getRandom() Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be choosen.

## Problem solution in Python.

```class Solution:

"""
Note that the head is guaranteed to be not null, so it contains at least one node.
"""
self.values=[]

def getRandom(self) -> int:
"""
Returns a random node's value.
"""
import random
return self.values[random.randint(0,len(self.values)-1)]
```

## Problem solution in Java.

```class Solution {

List<Integer> arr = null;
Random rand;

this.arr = new ArrayList<>();
while(ptr != null){
ptr = ptr.next;
}
this.rand = new Random();
}

/** Returns a random node's value. */
public int getRandom() {
return this.arr.get(this.rand.nextInt(this.arr.size()));
}
}
```

## Problem solution in C++.

```class Solution {
public:
vector<int>vec;
}
}
int getRandom() {
return vec[rand()%vec.size()];
}
};
```

## Problem solution in C.

```typedef struct {
} Solution;

srand(time(NULL));
Solution* sul = malloc(sizeof(Solution));
return sul;
}

int solutionGetRandom(Solution* obj) {

int count = 0;
struct ListNode* temp;
int i;
{
count++;
}

int  r= random()%count;
for(i=0;i<r;i++)
{
}