#100DaysOfCode – Day 89 Linked Lists & Big Number Addition 🔹 Problem: Add Two Numbers You’re given two non-empty linked lists representing numbers. Each node stores a single digit, and the digits are stored in reverse order. 🔹 My Approach: Used a dummy node to simplify list construction. Traversed both lists simultaneously while managing the carry. Created new nodes for each digit of the sum using modular arithmetic. Continued until both lists and carry were fully processed. Time Complexity: O(max(N, M)) Space Complexity: O(max(N, M)) Even in linked lists, clean logic + careful pointer handling can simplify complex problems. This challenge reinforced how breaking big tasks into smaller, consistent steps leads to efficient solutions. #takeUforward #100DaysOfCode #LinkedList #Java #ProblemSolving #LeetCode #DSA #CodingJourney #CodeNewbie #ChitkaraUniversity
Adding two numbers using linked lists in Java
More Relevant Posts
-
🗓 Day 6 / 100 – #100DaysOfLeetCode 📘 Problem: 3228. Maximum Number of Operations to Move Ones to the End Difficulty: Medium 💡 Problem Summary: Given a binary string s, you can repeatedly choose an index i where s[i] == '1' and s[i+1] == '0', and move that '1' to the right until it reaches the end of the string or hits another '1'. The goal is to find the maximum number of such operations possible. 🧠 My Approach: Instead of simulating the moves (which would be inefficient), I used a counting strategy: Keep a running count of the number of '1's seen so far (cnt). Whenever a '0' appears after one or more '1's, we can perform cnt operations involving those ones. Sum these up for the final result. 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(1) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
📌 Day 2/100 – Remove Element (LeetCode 27) 🔹 Problem: Given an integer array nums and a value val, remove all instances of that value in-place and return the new length of the array. The order of elements can be changed. 🔹 Approach: Used the two-pointer technique to efficiently modify the array in-place. One pointer iterates through the array, while the other tracks the position to overwrite non-val elements. Returned the position of the second pointer as the new length. 🔹 Key Learning: Strengthened understanding of in-place array manipulation. Improved logic building for pointer movement and conditional overwriting. Learned how to minimize extra space usage while maintaining readability and clarity. Another small yet powerful step toward mastering array-based problems! 💻 🔥 #100DaysOfCode #LeetCode #Java #ProblemSolving #TwoPointers #DSA #CodingJourney
To view or add a comment, sign in
-
-
Today’s problem: Merge Two Sorted Lists 🔗 Problem: Given two sorted linked lists, merge them into one sorted list and return it. Example: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Approach I used: ✅ Create a dummy node to simplify pointer operations. ✅ Use a pointer (tail) to build the merged list by comparing the heads of both lists. ✅ Append the smaller node each time and move forward. ✅ When one list ends, attach the remaining nodes of the other. A clean iterative solution that keeps the space usage minimal (O(1) extra space). ⚡
To view or add a comment, sign in
-
-
DSA Practice – Day 55 🚀 Problem: Middle of the Linked List (LeetCode 876) Problem Statement: Given the head of a singly linked list, return the middle node of the linked list. If there are two middle nodes, return the second one. 🧩 Brute Force Approach: Traverse the list once to count the total number of nodes. Then, traverse again to reach the middle node. Time Complexity: O(2n) ≈ O(n) Space Complexity: O(1) ⚡ Optimal Approach (Two Pointer Technique): Use two pointers: slow and fast. Move slow by 1 step and fast by 2 steps. When fast reaches the end, slow will be at the middle. Time Complexity: O(n) Space Complexity: O(1) ✨ What I Learned: The two-pointer technique makes traversal problems more efficient. It’s a simple yet powerful approach used in many linked list problems. #LeetCode #DSA #Java #LinkedList #ProblemSolving #CodingJourney #PlacementPrep
To view or add a comment, sign in
-
-
🔹 Day 38 – LeetCode Practice Problem: Missing Number (LeetCode #268) 📌 Problem Statement: You are given an array nums containing n distinct numbers taken from the range [0, n]. Find the one number that is missing from the array. ✅ My Approach (Java): Calculated the expected sum using the formula total = \frac{n \times (n + 1)}{2} The missing number = total - actual sum. This method avoids sorting or extra space, keeping it optimal. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Results: Accepted ✅ Runtime: 0 ms (Beats 100%) 🚀 Memory: 45.51 MB (Beats 32.44%) 💡 Reflection: A great reminder that sometimes the most efficient solutions come from simple mathematical reasoning. Clean, elegant, and lightning-fast! #LeetCode #ProblemSolving #Java #DSA #CodingPractice #Learning
To view or add a comment, sign in
-
-
🚀 Day 25 of 100 Days of LeetCode 📘 Problem: Remove Nth Node from End of List 💻 Language: Java ✅ Status: Accepted — Runtime: 0 ms ⚡ (Beats 100%) Today’s challenge was about working with Linked Lists, one of the most fundamental yet tricky data structures in DSA. The problem required removing the Nth node from the end — which tested how well I could use two-pointer techniques efficiently. ✨ Key Learnings: The two-pointer approach simplifies traversal logic beautifully 🎯 Dummy nodes are game-changers for clean code and edge case handling Linked Lists demand visualization — drawing it out helps a lot! Every accepted solution sharpens my ability to reason through edge cases and optimize thought flow 🔄 #Day25 #100DaysOfCode #LeetCode #Java #LinkedList #ProblemSolving #CodingJourney #DSA #SoftwareDevelopment #CodeEveryday #KeepLearning
To view or add a comment, sign in
-
-
📌 Day 38/100 – Make array elements equal to zero (LeetCode 3354) 🔹 Problem: Given an integer array nums, each element can be a number or zero. You need to find how many zeros in the array can be replaced by either +1 or -1 such that the total sum on both sides of that zero (left and right) remains balanced or differs by 1. 🔹 Approach: First, calculate the total sum s of all elements. Maintain a prefix sum l as you iterate. For each zero: If l * 2 == s, both +1 and -1 replacements are valid → add 2 to ans. If |l * 2 - s| == 1, only one replacement is valid → add 1 to ans. Return the total count ans. 🔹 Key Learning: Prefix sums simplify balance-based problems. Comparing 2 * prefixSum with total sum helps quickly check left-right equilibrium. 🔹 Complexity: Time: O(n) — single pass through array Space: O(1) — no extra storage used 🔹 Hashtags: #Day38Of100 #LeetCode3354 #100DaysOfCode #Java #DSA #ProblemSolving #PrefixSum #CodingChallenge
To view or add a comment, sign in
-
-
📌 Day 3/100 - Remove Duplicates from Sorted Array (LeetCode 26) 🔹 Problem: Given a sorted array, remove the duplicates in-place so that each element appears only once and return the new length. You must modify the array without using extra space for another array. 🔹 Approach: I used a simple counting-based approach: Iterate through the array using a single loop. If the current element is the same as the next, skip it. Otherwise, place it at the current count index and increment count. Finally, return count as the number of unique elements. 🔹 Key Learning: Practiced in-place array modification efficiently without extra space. Improved understanding of loop-based filtering logic. Realized that sometimes the simplest linear approach works best! Consistency compounds — each problem adds a new layer of confidence! 🚀#100DaysOfCode #LeetCode #Java #ProblemSolving #Array #DSA #TwoPointers
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟏𝟒 of #50DaysOfDSA 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝟏𝟔𝟏𝟏. 𝐌𝐢𝐧𝐢𝐦𝐮𝐦 𝐎𝐧𝐞 𝐁𝐢𝐭 𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐬 𝐭𝐨 𝐌𝐚𝐤𝐞 𝐈𝐧𝐭𝐞𝐠𝐞𝐫𝐬 𝐙𝐞𝐫𝐨 (𝐇𝐚𝐫𝐝) 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐋𝐢𝐧𝐤 : https://lnkd.in/g9NsEW8F 𝐒𝐨𝐥𝐮𝐭𝐢𝐨𝐧 𝐋𝐢𝐧𝐤 : https://lnkd.in/gKZGpX2B Today’s problem was a real brain-teaser that beautifully connects 𝐛𝐢𝐭 𝐦𝐚𝐧𝐢𝐩𝐮𝐥𝐚𝐭𝐢𝐨𝐧 with 𝐆𝐫𝐚𝐲 𝐜𝐨𝐝𝐞 𝐥𝐨𝐠𝐢𝐜 𝐓𝐡𝐞 𝐜𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞: Transform an integer n → 0 using minimum operations, where each operation allows you to flip certain bits based on specific rules. At first, it looks like a simulation problem... but the real trick lies in understanding how Gray codes work — where consecutive numbers differ by exactly one bit. 𝐈𝐧𝐭𝐮𝐢𝐭𝐢𝐨𝐧: We keep 𝐗𝐎𝐑𝐢𝐧𝐠 𝐧 with its 𝐫𝐢𝐠𝐡𝐭-𝐬𝐡𝐢𝐟𝐭𝐞𝐝 𝐯𝐚𝐥𝐮𝐞 until it becomes 0 , this effectively computes the minimal number of bit flips required. #LeetCode #Java #BitManipulation #CodingChallenge #DSA #ProblemSolving #DeveloperJourney #CodeNewbie #50DaysOfCode #WomenInTech #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 51 of My #LeetCode Challenge 🧩 Problem: Remove Nodes from Linked List (LeetCode #2487) 💻 Language Used: Java 🧠 Approach Used: Reverse the Linked List – This helps us process nodes from right to left, making it easy to track the greatest value seen so far. Traverse and Remove Nodes – While traversing the reversed list, if a node’s next value is smaller than the current max, we remove that node. Otherwise, we move forward and update max. Reverse the List Again – To restore the original order (but with the smaller nodes removed). ⏱️ Time Complexity: O(n) 👉 We reverse the list twice and traverse it once — all linear operations. 💾 Space Complexity: O(1) 👉 Done completely in place without using extra data structures. 🔥 This problem strengthened my understanding of linked list reversal and in-place node deletion logic. Every day is a step closer to mastering DSA! 💪 #Day51 #LeetCode #100DaysOfCode #Java #DSA #LinkedList #ProblemSolving #CodingChallenge
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
Kanhaiya Lal, love how you walked through the pointer logic here. That carry handling trick with modular arithmetic is exactly the kind of clean approach that makes linked list problems way less messy.