Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Linked List Random Node problem solution

YASH PAL, 31 July 202422 January 2026

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.
Leetcode Linked List Random Node problem solution

Leetcode Linked List Random Node problem solution in Python.

class Solution:

    def __init__(self, head: ListNode):
        """
        @param head The linked list's head.
        Note that the head is guaranteed to be not null, so it contains at least one node.
        """
        self.values=[]
        while head:
            self.values.append(head.val)
            head=head.next

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

Linked List Random Node problem solution in Java.

class Solution {

List<Integer> arr = null;
Random rand;

public Solution(ListNode head) {
    ListNode ptr = head;
    this.arr = new ArrayList<>();
    while(ptr != null){
        arr.add(ptr.val);
        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;
    Solution(ListNode* head) {
        while(head){
            vec.push_back(head->val);
            head = head->next;
        }
    }
    int getRandom() {
        return vec[rand()%vec.size()];
    }
};

Problem solution in C.

typedef struct {
    struct ListNode* head
} Solution;

Solution* solutionCreate(struct ListNode* head) {
    srand(time(NULL));
    Solution* sul = malloc(sizeof(Solution));
    sul->head=head;
    return sul;
}

int solutionGetRandom(Solution* obj) {
    
    int count = 0;
    struct ListNode* temp;
    struct ListNode* head;
    temp = obj->head;
    head = temp;
    int i;
    while(head!=NULL)
    {
        head=head->next;
        count++;
    }

    int  r= random()%count; 
    head = temp;
    for(i=0;i<r;i++)
    {
        head = head->next;
    }

    return head->val;
}

void solutionFree(Solution* obj) {
    free(obj);
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes