✨ Day 57 of 100: Rotate List ✨ Today’s challenge was LeetCode 61 – Rotate List 🔄 Problem: Given the head of a linked list, rotate the list to the right by k places. Example: Input: head = [1,2,3,4,5], k = 2 Output: [4,5,1,2,3] Key Idea: To rotate the list: 1️⃣ Find the length of the linked list. 2️⃣ Connect the tail to the head to make it circular. 3️⃣ Calculate the new head position as length - (k % length) steps from the start. 4️⃣ Break the circle at the right point to get the rotated list. 💡 Key Takeaways: Strengthened understanding of linked list traversal and manipulation. Learned to handle edge cases like k larger than the list length efficiently using modulo. Practiced converting a circular list back into a normal one at the right node. 💬 Every day of this challenge reinforces how small logical tweaks — like using % to handle rotations — can make code elegant and efficient. #100DaysOfCode #LeetCode #Java #LinkedList #CodingChallenge #DSA #ProblemSolving #WomenWhoCode
Rotating a Linked List with LeetCode Challenge
More Relevant Posts
-
🚀 Just solved LeetCode 30 – Substring With Concatenation of All Words. 🧩 What was the problem? Given a string and a list of words (all same length), you have to find every starting index where all the words appear together as a continuous substring, in any order. Basically a sliding-window puzzle with fixed word sizes and frequency matching. 🛠️ What was my approach? • Counted all words using a map • Used multiple sliding windows based on word length • Expanded the window word by word • If a word appeared more than needed, I moved the left pointer forward • Added the index when the window matched every word exactly The key was treating the string in equal chunks, not character by character. 📚 What I learned Working with strings becomes much easier when you break the problem into predictable pieces. Managing window boundaries is the real challenge here, and walking through tiny examples helps more than you think. #LeetCode #Java #DSA #CodingPractice #ProblemSolving
To view or add a comment, sign in
-
-
✨ Day 54 of 100: Reverse Nodes in k-Group ✨ Today’s challenge was LeetCode 25 – Reverse Nodes in k-Group 🔁 🧩 Problem: Given a linked list, reverse the nodes of the list k at a time and return its modified list. If the number of nodes is not a multiple of k, the remaining nodes should remain as-is. 💡 Key Takeaways: 🔹 Strengthened understanding of linked list manipulation and pointer handling. 🔹 Practiced breaking the list into segments of size k and reversing each group independently. 🔹 Learned how to manage head and tail connections between reversed and remaining parts. 🔹 Reinforced problem-solving using dummy nodes to simplify list operations. 🧠 Approach: 1️⃣ Count the total nodes to know how many full groups of k exist. 2️⃣ For each group, reverse k nodes iteratively. 3️⃣ Connect the reversed group to the next segment. 🧭 Insight: This problem beautifully illustrates how iteration with precise pointer movement can replace recursion while still achieving elegant reversals in linked lists. #100DaysOfCode #Day53 #LeetCode #Java #LinkedList #CodingJourney #DSA #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 73 of #100DaysOfCode Today I solved LeetCode 3346 – Maximum Frequency of an Element After Performing Operations I 🧩 This problem was all about logical range manipulation — figuring out how many elements could be converted into the same number when each element can be modified by ±k, but with a limited number of allowed operations. At first, it felt similar to the “most frequent element” sliding window problem, but the twist here was controlling how many elements can be changed, not just the total cost. After a few failed attempts (and a battle with edge cases 😅), I finally implemented a correct solution using the difference array + prefix sum approach. 💡 Key Takeaways: Think in terms of ranges, not just differences. Prefix-sum (difference map) logic is incredibly powerful for interval counting. Always recheck constraints — “number of operations” vs “total cost” makes a huge difference! Here’s a snippet from my final Java solution: int achievable = Math.min(running, same + numOperations); ans = Math.max(ans, achievable); It’s small details like these that make problem-solving such a rewarding process 💪 #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #DSA #LearningEveryday
To view or add a comment, sign in
-
-
✅ Day 51 of LeetCode Medium/Hard Edition Today’s challenge was about generating numbers whose prime factors are limited to 2, 3, and 5 — the classic Ugly Number II problem! 💫 📦 Problem: An ugly number is a positive integer whose prime factors are restricted to 2, 3, and 5. Given an integer n, return the nth ugly number. 🔗 Problem Link: https://lnkd.in/gS9EKbbd ✅ My Submission: https://lnkd.in/gnTe_zvY 💡 Thought Process: To generate the sequence in ascending order, we can use a Min-Heap + HashSet approach: Start from 1, the first ugly number. Repeatedly multiply it by {2, 3, 5} to generate new candidates. Maintain a min-heap to always pick the next smallest number. Use a set to avoid duplicates. After popping n elements, the last popped value is our nth ugly number. ⏱ Complexity: Time: O(n log n) Space: O(n) 🔥 Key Takeaway: Combining heaps and hashing efficiently maintains order and uniqueness in sequence generation. A great demonstration of how priority queues can systematically build ordered numeric sequences — step by step! #LeetCodeMediumHardEdition #100DaysChallenge #Heap #HashSet #ProblemSolving #DataStructures #CodingJourney #Java #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 20 of #Blind75 Challenge 🎯 Problem: Merge Two Sorted Lists 🧩 Platform: LeetCode (#21) 💡 Category: Linked List 🧠 Problem Statement Given the heads of two sorted linked lists, merge them into a single sorted linked list and return its head. Example: Input: List1 = 1 → 2 → 4 List2 = 1 → 3 → 4 Output: 1 → 1 → 2 → 3 → 4 → 4 💡 Intuition This problem is all about two-pointer merging, similar to how you merge arrays in merge sort. We use a dummy node to simplify pointer handling. Then, at each step: Compare list1.val and list2.val Attach the smaller one to the merged list Move that list’s pointer forward Finally, append the remaining nodes. ⚙️ Complexity ⏱ Time: O(m + n) 💾 Space: O(1) ✨ Key Takeaway A clean example of how a dummy node simplifies linked list merging. This logic forms the foundation for many advanced problems like “Merge K Sorted Lists” and “Sort Linked List.” ✅ Day 20 of #Blind75 completed! Next up → Linked List Cycle Detection 🔁 📚 Full solutions available at my LeetCode profile: 👉 https://lnkd.in/gGEwKNFV #Blind75 #LeetCode #Java #LinkedList #DSA #CodingChallenge #ProblemSolving #CareerGrowth #100DaysOfCode #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 118 of 120 – #DSAwithJava Challenge Hey LinkedIn fam! 👋 Today’s problem was all about Linked Lists and HashSets — a combination that makes node deletions efficient and elegant ⚡ ✅ Problem: Delete Nodes From Linked List Present in Array (LeetCode #3217 – Medium) 🎯 Objective: Given an array nums and the head of a linked list, remove all nodes whose values exist in nums. 🧠 Key Insight: Store all elements of nums in a HashSet for O(1) lookups. Use a dummy node before the head to handle edge deletions easily. Traverse the list, and if a node’s value exists in the set, skip it by adjusting pointers. The result is a clean linked list containing only valid nodes. 💡 Example: Input → nums = [1,2,3], head = [1,2,3,4,5] Output → [4,5] 🕒 Time Complexity: O(n + m) 📦 Space Complexity: O(m) 🔥 Takeaway: When dealing with linked lists, HashSets simplify membership checks, and dummy nodes simplify pointer logic. A small design tweak can lead to clean and optimal solutions! 💪 #120DaysOfCode #DSAwithJava #LeetCode #Java #LinkedList #HashSet #ProblemSolving #CodingChallenge #TechJourney #Consistency #LearnByDoing
To view or add a comment, sign in
-
-
🚀 Day 11 of 100 Days of LeetCode! 💻 Today’s problem: Implement strStr() 🔍 🧩 Problem #28: Find the Index of the First Occurrence in a String This problem is a classic example of string pattern matching, where we need to find the starting index of a substring (needle) in a given string (haystack). ✨ My Approach: Used a simple sliding window technique to check each substring of haystack with length equal to needle. Compared it directly using equals() to find a match. Time complexity: O((n - m + 1) * m), which is acceptable for moderate input sizes. ✅ Result: All test cases passed successfully — and achieved 100% runtime efficiency ⚡ Each day I’m learning to think more efficiently and write cleaner, more optimized code. Consistency really is the key 🔑 #Day11 #100DaysOfCode #LeetCode #Java #ProblemSolving #CodingJourney #SoftwareDevelopment #DSA
To view or add a comment, sign in
-
-
🌟 Problem 21 – Merge Two Sorted Lists I solved this problem today on LeetCode 💪. It helped me understand how to work with linked lists and how to merge them efficiently. 🧩 Description: Given two sorted linked lists, merge them into one sorted list and return its head. 👉 Example: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] ✅ Approach: * Compare nodes from both lists one by one. * Add the smaller value to the new list. * Continue until all elements are merged. 📈 Time Complexity: O(n + m) 📦 Space Complexity: O(1) #LeetCode #TopInterview150 #Java #DSA #Coding #ProblemSolving #LinkedList
To view or add a comment, sign in
-
-
✅Day 41 : Leetcode 153 - Find Minimum in Rotated Sorted Array #60DayOfLeetcodeChallenge 🧩 Problem Statement Given a sorted array that has been rotated at an unknown pivot, find the minimum element in the array. The array contains unique elements, and the solution must run in O(log n) time. 💡 My Approach I used a binary search technique to efficiently find the minimum element. I maintained two pointers, low and high. At each step, I calculated the mid-point. If the left part (nums[low] to nums[mid]) was sorted, I updated my answer with the smaller of nums[low] and current ans, and moved low to mid + 1. Otherwise, I updated my answer with nums[mid] and moved high to mid - 1. This approach ensures we keep narrowing the search space toward the minimum element. ⏱️ Time Complexity O(log n) — Because the search space is halved in each iteration. #BinarySearch #LeetCode #RotatedSortedArray #DSA #CodingPractice #Java #ProblemSolving
To view or add a comment, sign in
-
-
💻 LeetCode 50 Days Challenge — Day 2: Longest Common Prefix Day 2 of my #LeetCode50DaysChallenge ✅ Today’s problem was all about string manipulation — Longest Common Prefix ✨ 🧩 Problem: Given an array of strings, find the longest common prefix among them. If there’s no common prefix, return an empty string "". 💡 Approach: I sorted the array alphabetically, then compared the first and last strings (since they’ll have the maximum difference). By comparing characters until they differ, I was able to build the common prefix using a StringBuilder. This approach is both elegant and efficient for the problem! ⏱️ Time Complexity: O(n × m) — where n is the number of strings and m is the length of the shortest string. 📊 Example: Input: ["flower", "flow", "flight"] Output: "fl" This problem really highlights how a simple sorting trick can simplify logic significantly. Feeling more confident with string problems — consistency is the key! 🔥 #LeetCode #CodingChallenge #Day2 #ProblemSolving #Java #SoftwareDevelopment #Consistency #100DaysOfCode
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development