Category: Java programming question
-
Problem 258: Add Digits
You are given a non-negative integer num.Your task is to repeatedly add all its digits until only one digit remains. This final single digit is also known as the digital root. Understanding the Problem You repeatedly sum the digits of the number: Example:num = 383 + 8 = 111 + 1 = 2So the answer…
-
Problem 25: Reverse Nodes in k-Group
You are given the head of a linked list and an integer k.Your task is to reverse the nodes of the list k at a time, and return the modified list. If the number of nodes remaining at the end is less than k, leave them as-is. Understanding the Problem Example: Input: 1 → 2…
-
Problem 258: Add Digits
Given a non-negative integer num, you must repeatedly add all its digits until only one digit remains. This is known as the digital root of a number. Example: Input: 383 + 8 = 111 + 1 = 2Output = 2 Understanding the Problem You repeatedly sum the digits of a number: If the number has…
-
Problem 154: Find Minimum in Rotated Sorted Array II
This is a follow-up to LeetCode 153, but with one important difference: The rotated sorted array may contain duplicates. Example: Input: [2,2,2,0,1]Output: 0 Because duplicates exist, the classic binary-search logic from problem 153 does not work directly and needs modification. Understanding the Problem The array is: SortedRotated at some pivotContains duplicates Goal: return the minimum…
-
Problem 153: Find Minimum in Rotated Sorted Array
You are given a rotated sorted array with unique elements.Your task is to return the minimum value in this array. A rotated sorted array is formed by taking a sorted array and rotating (shifting) it some number of times. Example:Original sorted array: [0, 1, 2, 4, 5, 6, 7]After rotation: [4, 5, 6, 7, 0,…
-
Problem 797: All Paths From Source to Target
You are given a directed acyclic graph (DAG). The graph is represented as an adjacency list, where: graph[i] = list of all nodes you can go to directly from node i. Your task is to return all possible paths from node 0 (source) to node n−1 (target). Understanding the Problem Example: graph =[[1,2],[3],[3],[]] This means:…
-
Problem 260: Single Number III
You are given an integer array where every element appears exactly twice except for two elements which appear only once.Your task is to return those two unique numbers. Example: Input: [1,2,1,3,2,5]Output: [3,5]Explanation:Numbers 1 and 2 appear twice.Numbers 3 and 5 appear once. Order does not matter. Understanding the Problem This is a variation of the…
-
Problem 103: Binary Tree Zigzag Level Order Traversal
You are given the root of a binary tree.Your task is to return its level order traversal, but with a twist: Levels must alternate between left-to-right and right-to-left. This pattern is commonly called a zigzag or spiral traversal. Understanding the Problem Example tree: Expected output: [[3],[20, 9],[15, 7]] Explanation:Level 0 → left to right →…
-
Problem 79: Word Search
You are given a 2D board of characters and a word.Your task is to determine whether the word exists in the board. A word exists if it can be constructed from sequentially adjacent cells.Valid moves are only horizontal and vertical (no diagonal).The same cell cannot be used more than once. Understanding the Problem Example: Board:…
-
Problem 203: Remove Linked List Elements
Understanding the Problem A linked list consists of nodes, and each node has:value → node.valpointer → node.next Your job is to traverse the list and skip all nodes having a specific value. Example: Input: head = [1,2,6,3,4,5,6], val = 6Output: [1,2,3,4,5]Explanation: All nodes containing 6 must be removed. Key point:The node to be removed may…
