/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution { ListNode head;
Random random;
/** @param head The linked list's head.
Note that the head is guaranteed to be not null, so it contains at least one node. */
public Solution(ListNode head)
{
this.head = head;
random = new Random();
} /** Returns a random node's value. */
public int GetRandom()
{
ListNode c = this.head;
int r = c.val;
for (int i = ; c.next != null; i++)
{
c = c.next;
if (random.Next(i + ) == i)
{
r = c.val;
}
} return r;
}
} /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.GetRandom();
*/

https://leetcode.com/problems/linked-list-random-node/#/description

leetcode382的更多相关文章

  1. [Swift]LeetCode382. 链表随机节点 | Linked List Random Node

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

随机推荐

  1. 【剑指offer】顺时针打印矩阵,C++实现

    原创文章,转载请注明出处! 博客文章索引地址 1.题目 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵,则依次打印出数字1,2,3,4,8,12,16,15,14 ...

  2. librec库

    固定初始化矩阵值 net.librec.math.structure -> class DenseMatrix -> void init()

  3. Win7 使用密码共享磁盘连接总是提示输入密码

    Win7 使用密码共享磁盘连接总是提示输入密码,只要设置下面这里就可以了.默认是保持来宾身份.

  4. Backward Digit Sums

    FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N < ...

  5. waitKey()

    waitKey仅对窗口机制起作用,即namedWindow产生的窗口.若在此之前没有产生窗口,则waitKey相当于未执行. 注:namedWindow产生的窗口: namedWindow()+ims ...

  6. Appium+python(1)简单的介绍环境搭建

    环境搭建其实并不难,只不过安装的东西有点多,要加的环境变量有点多. 链接:https://pan.baidu.com/s/1nwLhNIT 密码:56wn 这个压缩包里要用的都有了,只需要下载,然后安 ...

  7. eclipse安装最新版svn

    1.下载最新的Eclipse,我的版本是3.7.2 indigo(Eclipse IDE for Java EE Developers)版    如果没有安装的请到这里下载安装:http://ecli ...

  8. Nchan 实时消息ha 配置

    备注:      Nchan 的数据持久化,以及ha 都是通过redis实现的,如果要做到无单点可以使用redis cluster     同对于Nchan server 进行多副本   1. 安装 ...

  9. consul 几个方便使用的类库

    consul 几个方便使用的类库 1. java  https://github.com/OrbitzWorldwide/consul-client   <dependency> < ...

  10. php基础语法(控制语句、数组、函数)

    流程控制 if -else if -else语句: switch语句: while循环: do while循环 for循环: 控制脚本执行进度 die(“输出内容”) exit是die的同义词. sl ...