Skip to main content

Posts

Leetcode 26: Remove Duplicates from Sorted Array

  1. Problem Statement (Simple Explanation): You are given an integer array nums that is sorted in  non-decreasing  order. You must: Remove duplicates in-place  so that each unique element appears  only once . Keep the  relative order  of unique elements. Return k, the  number of unique elements . After your function: The  first k elements  of nums should contain the unique elements in sorted order. Anything beyond index k - 1 can be ignored. You  must  use only constant extra space O(1). 2. Examples: Example 1: Input: nums = [1,1,2] Unique elements are [1,2]. Output: k = 2, nums = [1,2,_] (Underscore means “don’t care” beyond k.) Example 2: Input: nums = [0,0,1,1,1,2,2,3,3,4] Unique elements: [0,1,2,3,4]. Output: k = 5, nums = [0,1,2,3,4,_,_,_,_,_] Constraints: 1 <= nums.length <= 3 x 10 4 -100 <= nums[i] <= 100 nums is sorted in non-decreasing or...
Recent posts

Leetcode 25: Reverse Nodes in k-Group

  1. Problem Statement (Simple Explanation): You’re given the head of a singly linked list and an integer k. You must: Reverse the nodes of the list  k at a time . If the number of nodes at the end is  less than k , leave that tail part  as is  (not reversed). You must  not modify node values , only  relink nodes . Return the head of the modified list. 2. Examples: Example 1: Input: head = [1,2,3,4,5], k = 2 Process in groups of 2: Group 1: [1,2] → [2,1] Group 2: [3,4] → [4,3] Remaining node: [5] (less than k) → unchanged Output: [2,1,4,3,5] Example 2: Input: head = [1,2,3,4,5], k = 3 Process in groups of 3: Group 1: [1,2,3] → [3,2,1] Remaining nodes: [4,5] (less than k) → unchanged Output: [3,2,1,4,5] Constraints: Number of nodes = n 1 <= k <= n <= 500 0 <= Node.val <= 10000 Follow-up:  Solve using O(1) extra space (i.e., in-place, no e...