🌟 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
Merging Two Sorted Linked Lists in Java
More Relevant Posts
-
🚀 Day 56 of my #100DaysOfLeetCode Challenge 🚀 Today's problem: Matrix Diagonal Sum Language: Java In this challenge, I practiced calculating the sum of both primary and secondary diagonals of a square matrix. The goal was to improve my understanding of nested loops and conditional logic for index-based problems. Key Takeaways: Strengthened logic for 2D array traversal Improved debugging and edge case handling Reinforced clean, readable code habits Here’s the core idea from my solution: If i == j (primary diagonal) or i + j == n - 1 (secondary diagonal), add that element to the sum. #LeetCode #Java #CodingChallenge #100DaysOfCode #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
💡 LeetCode #58 — Length of Last Word Today I solved LeetCode Problem 58: Length of Last Word 🧠 Problem Summary: Given a string s consisting of words and spaces, return the length of the last word in the string. A word is defined as a sequence of non-space characters. Key Idea: We can traverse the string from end to start — Skip trailing spaces. Count the number of characters until the next space (which marks the last word). This avoids unnecessary splitting and saves extra space. #LeetCode #Java #DSA #Strings #ProblemSolving #CodingChallenge
To view or add a comment, sign in
-
-
🚀 Day 63 of My LeetCode journey 🚀 Problem : Custom Sort String Today’s problem was interesting because instead of sorting using normal alphabetical order, we sort the given string based on a custom priority order. 🧠 Problem Understanding Given: order → defines priority of characters. str → the input string which needs to be rearranged. Goal: ✅ Arrange characters of str based on the sequence defined in order. ✅ Characters not present in order should appear at the end (any order). 💡 Approach (Simple & Efficient) Count frequency of each character in str. First append characters following the order. Then append remaining characters. Time Complexity: O(n) Space Complexity: O(1) (fixed array size for 26 letters) ✨ Learnings Sometimes the problem isn't about sorting, but about following a custom priority. Frequency counting is extremely powerful for character-based problems. StringBuilder → best way to build strings efficiently in Java. #leetcode #coding #dsa #java #100DaysOfCode #day63 #learningEveryday #problemSolving
To view or add a comment, sign in
-
-
🚀 Day 43 of #100DaysOfLeetCode Today's problem: LeetCode #160 – Intersection of Two Linked Lists 💡 Concept: Find the node where two singly linked lists intersect. Used the Two Pointer Approach — a smart and efficient way to solve this without using extra memory. 🧠 Logic: Move both pointers through the lists. When one pointer reaches the end, switch it to the other list’s head. They’ll either meet at the intersection node or end up as null together. ✅ Complexity: Time – O(n + m) Space – O(1) 💬 Takeaway: Sometimes, the best solutions come from balancing the path — literally! Understanding how pointers sync up teaches a lot about memory references and linked list behavior. #LeetCode #CodingChallenge #Java #DataStructures #TwoPointerTechnique #ProblemSolving #LinkedLists
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
-
-
🚀 Day 33 of My Daily Coding Challenge 🚀 Today I implemented and revised Cyclic Sort (Java) — one of the most efficient sorting techniques for arrays containing numbers from 1 to N. 💡 Key takeaway: Instead of comparing adjacent elements, Cyclic Sort keeps placing each number at its correct index — making it super fast for such problems. 🔗 Solution: [https://lnkd.in/epUjThMt] Building consistency, one algorithm at a time 💻💪 #DailyCodingChallenge #Java #DSA #CyclicSort #SortingAlgorithms #100DaysOfCode #LearningEveryday
To view or add a comment, sign in
-
-
💡 LeetCode #15 — 3Sum Today I solved LeetCode Problem 15: 3Sum 🔍 Problem Summary: Given an integer array nums, return all unique triplets (a, b, c) such that: a + b + c = 0 The solution must not contain duplicate triplets. Key Idea: Use sorting + two pointers to reduce the time from O(n³) to O(n²). Steps: Sort the array. Fix one number (nums[f]) in each iteration. Use two pointers (i and j) to find pairs whose sum equals -nums[f]. Skip duplicates for both the fixed index and the pointer values. This efficiently finds all unique triplets. #LeetCode #Java #DSA #TwoPointers #ProblemSolving #CodingChallenge #Algorithms
To view or add a comment, sign in
-
-
💻 LeetCode Challenge – Day 6: Merge Two Sorted Lists Today’s challenge was all about linked lists — merging two sorted lists into one sorted list 🔗 🔹 Problem: Given two sorted linked lists, merge them into a single sorted linked list and return it. 🔹 Key Learnings: ✅ Deepened understanding of linked list traversal and pointers ✅ Practiced building a dummy node approach for cleaner logic ✅ Learned how to handle edge cases efficiently ✅ Reinforced the importance of iterative vs recursive thinking This problem really helped me think more clearly about data structure manipulation and clean code design. 💪 #LeetCode #100DaysOfCode #Java #CodingChallenge #ProblemSolving #LinkedList #MergeTwoSortedLists #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 77/100 of #100DaysOfLeetCode Today I solved Binary Search (LeetCode Problem 704), a classic algorithmic challenge and an important concept for efficient searching. The task was to determine the index of a target value in a sorted array using an algorithm with O(log n) time complexity. If the target exists, return its index; otherwise return -1. 💡 Key Takeaway: Binary search is a fundamental algorithm that helps build strong problem-solving intuition and improves understanding of time complexity. #100DaysOfCode #LeetCode #CodingJourney #Java #DSA #BinarySearch #KeepLearning #ProblemSolving
To view or add a comment, sign in
-
-
#Day_33 Today’s challenge was an interesting one — "Find the Duplicate Number" Given an array of integers where each number is between 1 and n, with one number repeating — the goal is to identify that duplicate efficiently. Approach: Create a boolean array seen[] to track which numbers have appeared. As we iterate through the array, if we encounter a number already marked as true, that’s our duplicate! Complexity: Time: O(n) Space: O(n) (due to the boolean array) Though this works efficiently, I also explored the Floyd’s Tortoise and Hare approach for a space-optimized version — a fascinating concept using cycle detection Sometimes the simplest approach is the clearest — but exploring alternatives is what sharpens your problem-solving mindset. #LeetCode #CodingChallenge #Java #ProblemSolving #100DaysOfCode #MudassirCodes
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