版本比较

标识

  • 该行被添加。
  • 该行被删除。
  • 格式已经改变。

...

代码块
int getRandom(ListNode *head) {
    int ans;
    int i = 01;
    for(auto node = head; node != nullptr; node = node->next) {
        if(rand() % i == 0) { // 第i个结点选中的概率为1/i,如果命中
            ans = node->val;
        }
        i++;
    }
    return ans;
}

...