LeetCode - 150 - Diameter of Binary Tree

The problem Given the root of a binary tree, return the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. The length of a path between two nodes is represented by the number of edges between them. Examples ...

July 26, 2025 · 3 min · Dmytro Chumakov

LeetCode - 150 - LRU Cache

The problem Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class: LRUCache(int capacity) Initialize the LRU cache with positive size capacity. int get(int key) Return the value of the key if the key exists, otherwise return -1. void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key. The functions get and put must each run in O(1) average time complexity. ...

July 22, 2025 · 5 min · Dmytro Chumakov

LeetCode - 150 - Find the Duplicate Number

The problem Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums, return this repeated number. You must solve the problem without modifying the array nums and using only constant extra space. Examples Input: nums = [1,3,4,2,2] Output: 2 Input: nums = [3,1,3,4,2] Output: 3 Input: nums = [3,3,3,3,3] Output: 3 Constraints 1 <= n <= 10^5 nums.length == n + 1 1 <= nums[i] <= n All the integers in nums appear only once except for precisely one integer which appears two or more times. Follow up: ...

July 17, 2025 · 4 min · Dmytro Chumakov

LeetCode - 150 - Add Two Numbers

The problem You are given two non‑empty linked lists representing two non‑negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Examples Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807. Input: l1 = [0], l2 = [0] Output: [0] Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] Output: [8,9,9,9,0,0,0,1] Constraints The number of nodes in each linked list is in the range [1, 100]. 0 ≤ Node.val ≤ 9 It is guaranteed that the list represents a number that does not have leading zeros. Explanation When we add two numbers, we need to remember the carry value when it’s necessary. The main caveat in this problem is the edge cases; we will discuss them later. From the description of the problem, we know that we are given two non‑empty linked lists without negative integers, and the digits are stored in reverse order; we will see later that this reverse order will help us solve the problem. ...

July 14, 2025 · 3 min · Dmytro Chumakov

LeetCode - 150 - Copy List with Random Pointer

The problem A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null. Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointers of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list. ...

July 13, 2025 · 3 min · Dmytro Chumakov