🚀 Day 23 / 100 | Add Two Numbers -Approach : O(n) - Traverse both linked lists simultaneously. - Maintain a carry variable to handle sums that are greater than or equal to 10. - Use a ans node to simplify the construction of the result list. - At each step, compute the sum as: sum = val1 + val2 + carry. - Update the carry as carry = sum / 10 and store sum % 10 in a new node. - Continue this process until both lists and the carry are exhausted. - Finally, return ans.next. #100DaysOfCode #LeetCode #Java #DSA #LinkedList #ProblemSolving
KOUSHIK THOTA’s Post
More Relevant Posts
-
🚀 Day 33 / 100 | Partition List -Intuition: This problem is based on Linked List partitioning. We are given the head of a linked list and a value x. Goal is to rearrange the list such that all nodes with values less than x come before nodes with values greater than or equal to x. we must preserve the original relative order of nodes in each partition. "So instead of modifying values, we create two separate lists and then connect them". -Approach: O(n) Initialize two dummy nodes: l1 and l2. These will store nodes less than x and nodes greater than or equal to x respectively. Traverse the original linked list from head to null. If current node value < x, attach it to the small list. Otherwise, attach it to the greater list. Move the pointers accordingly. After traversal, connect the end of the small list to the start of the greater list. Make sure to set greater.next = null to avoid cycle. Return l1.next as the new head. -Complexity: Time Complexity: O(n) Space Complexity: O(1) #100DaysOfCode #Java #DSA #LeetCode #LinkedList
To view or add a comment, sign in
-
-
Day 4 of #60daysofLeetcode, #7. Reverse Integer - https://lnkd.in/dqA_ciDP is the question. Solution. To reverse the integer, we repeatedly extract the last digit using modulo 10, then build the result from left to right. Each iteration: extract the last digit, update result = result * 10 + digit, and divide input by 10. We continue until the input becomes zero. Before each result update, we check if the operation would cause overflow, and if so, return 0. Time Complexity is O(log n) or O(d) where d is the number of digits. Space complexity is O(1). #LeetCode, #Java, #DSA, #LearningInPublic
To view or add a comment, sign in
-
-
Day 15/100 – LeetCode Challenge Problem: Reverse Linked List Today’s problem focused on reversing a singly linked list using an iterative approach. Approach: Used three pointers to reverse the links: prev → keeps track of the previous node curr → current node being processed next → stores the next node before changing the link Steps: Store curr.next in next Reverse the link → curr.next = prev Move prev and curr one step forward Continue until the list ends. Finally, prev becomes the new head of the reversed list. Complexity: Time: O(n) Space: O(1) Concepts Practiced: Linked List pointer manipulation Iterative reversal technique In-place algorithm #100DaysOfCode #LeetCode #DSA #Java #LinkedList #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 42 / 100 | Remove Duplicates from Sorted List II -Intuition: -The linked list is sorted, so duplicate values appear next to each other. -The goal is to remove all nodes that contain duplicate numbers and keep only distinct values. -To handle cases where duplicates appear at the beginning of the list, use a temp node before the head. -When a duplicate sequence is detected, we skip all nodes with that value. -Approach: O(n) -Create a temp node pointing to the head of the linked list. -Use two pointers: prev (last confirmed unique node) and curr (current node). -Traverse the linked list while checking if the current node has duplicates. -If duplicates are found, skip all nodes with that value. -Update prev.next to the next non-duplicate node. -If no duplicate is found, move both pointers forward. -Repeat the process until the end of the list. -Complexity: Time Complexity: O(n) Space Complexity: O(1) #100DaysOfCode #Java #DSA #LeetCode #linkedlist
To view or add a comment, sign in
-
-
🔢 Day 67/100: Convert to Hex - Bit Masking Day 67. Convert integer to hex. Labeled "EASY." Actually easy this time. ✅ 📌 Problem: Convert number to hexadecimal string. 💡 Solution: & 15 → Get last 4 bits (0-15) Map to hex char (0-9, a-f) >>> 4 → Shift right 4 bits Repeat, reverse result 🎯 The Key: num & 15 extracts last hex digit. num >>>= 4 moves to next digit. 📊 Complexity: O(1) - at most 8 hex digits Day 67. Bit manipulation ftw. 💪 #100DaysOfCode #DSA #LeetCode #Day67 #Java #HexConversion #BitManipulation
To view or add a comment, sign in
-
-
🚀 Day 37 / 100 | Smallest Pair With Different Frequencies -Intuition: -This problem is based on frequency counting and greedy selection. -So the idea is to count the frequency of each number and then check pairs in increasing order. -Approach: O(n log n) -First, store the frequency of each number using HashMap. -Then, store all distinct numbers in a list. -Sort the list so we can get the smallest values first. -Now, check every pair (x, y) where x < y: If freq(x) != freq(y), return that pair immediately. -Since the list is sorted, the first valid pair will be the answer. -Complexity: Time Complexity: O(n log n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 15 of #100DaysOfCode Solved Reverse Linked List on LeetCode 🔄🔗 🧠 Key insight: Reversing a linked list is all about careful pointer manipulation. By tracking prev, current, and next, we can reverse links in-place without extra memory. ⚙️ Approach: 🔹Initialize prev = null, current = head 🔹Store next node before breaking the link 🔹Reverse current.next to point to prev 🔹Move pointers forward until the list ends ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #LinkedList #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
#day327 of #1001daysofcode problem statement (1582): Special Positions in a Binary Matrix -First counted the number of 1s in each row and column, then identified positions where both counts were exactly one. -Brute force tc=O(m*n*(m+n)), sc=O(1) -Reduced repeated checks and brought the solution down to O(m × n) but it costs some space. sc=O(m+n) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
Day 85/100: Construct Tree from Inorder & Postorder Day 85. MEDIUM. Last element of postorder = root. Split inorder. Recurse. Build tree backwards. ✅ 📌 Problem: Build binary tree from inorder and postorder traversals. 💡 Solution: Postorder's last = root Find root in inorder → splits left/right Recurse on both sides with correct indices 🎯 The Logic: Postorder: Left → Right → Root (last is always root!) Inorder: Left → Root → Right (root splits sides) 📊 Complexity: O(n²) time (can optimize with HashMap) Day 85. Tree construction. 🌳 #100DaysOfCode #DSA #LeetCode #Day85 #Java #TreeConstruction
To view or add a comment, sign in
-
-
Day 30 — LeetCode Progress (Java) Rotate Array Required: Rotate the array to the right by k steps efficiently without using extra space. Idea: Instead of shifting elements one by one, use array reversal to reposition elements in optimal order. Approach: Reverse the entire array Reverse first k elements Reverse remaining elements Time Complexity: O(n) Space Complexity: O(1) #LeetCode #DSA #Java #CodingJourney #ProblemSolving #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