Day - 50 Intersection of Two Linked Lists The problem - Given the heads of two singly linked lists, return the node at which the two lists intersect. If they don't intersect, return null. Example : listA = [4,1,8,4,5], listB = [5,6,1,8,4,5] → true, intersect at node 8 Brute force - Use HashSet to store nodes from one list, check second list, but the space complexity will be O(n). Approach Used - •) Initialize two pointers: listA = headA, listB = headB. •) While (listA != listB), 1 - If listA != null, move to next, listA = listA.next, else switch to headB. 2 - If listB != null, move to next: listB = listB.next, else switch to headA. •) Return listA. Complexity - Time - O(m + n), both pointers traverse at most m+n nodes. Space - O(1), only used pointers. Note - By switching heads, each pointer travels exactly the same total distance (Length A + Length B). This syncs them up so they hit the intersection at the exact same time! #DSA #Java #SoftwareEngineering #InterviewPrep #LearnToCode #CodeDaily #ProblemSolving
Finding Intersection of Two Linked Lists
More Relevant Posts
-
🚀 Day 70 of #100DaysOfLeetCode Problem: 4Sum Difficulty: Medium Key Concept Learned: Sorting + Two Pointers + Duplicate Handling Today’s challenge was about finding all unique quadruplets in an array whose sum equals a given target. The optimized approach involved sorting the array, fixing two indices, and then using the two-pointer technique to efficiently search for valid combinations. Key takeaways: Sorting enables efficient pointer movement and duplicate detection Skipping duplicates at every level (i, j, left, right) is crucial to ensure uniqueness Two-pointer technique reduces the brute force complexity significantly Careful pointer handling avoids repeated results and improves performance A great problem for strengthening array manipulation, pointer logic, and edge-case handling. On to Day 71! 💪🔥 #LeetCode #DSA #Java #TwoPointers #Arrays #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟵/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Solve Merge Sorted Array using the Two Pointer (from end) technique. ➤ Approach (O(m + n), O(1) space): Set three pointers: —> i = m - 1 (end of nums1 valid elements) —> j = n - 1 (end of nums2) —> k = m + n - 1 (end of nums1 total capacity) Compare elements from the back and place the larger one at index k Move pointers accordingly until one array is exhausted ➤ Key Insight: Merging from the front would overwrite values. Merging from the back avoids extra space and keeps everything in-place. #LeetCode #Java #DSA #TwoPointers #ArrayProblems #ProblemSolving #20DaysChallenge #Consistency
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 117 Subsets (Power Set) using Bit Manipulation Problem: LeetCode 78 – Subsets Task:- Given an integer array of unique elements, return all possible subsets (the power set). Example: Input: nums = [1, 2, 3] Output: [[], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3]] My Approach (Bit Manipulation): Observed that for an array of size n, total subsets = 2^n. Used numbers from 0 to (2^n - 1) as binary masks. Each bit position represents whether an element is included (1) or excluded (0) in the subset. Iterated through each mask and built subsets accordingly. Complexity: Time Complexity: O(n * 2^n) Space Complexity: O(2^n) Bit manipulation is not just about low-level tricks it can provide elegant and efficient solutions to problems like generating subsets. Once you see the binary pattern, the solution becomes surprisingly intuitive. #LeetCode #Java #BitManipulation #DSA #ProblemSolving #TakeUForward #CodingJourney #200DaysOfCode #PowerSet #CleanCode
To view or add a comment, sign in
-
-
This problem uses a modified Binary Search to find a target in a rotated sorted array in O(log n) time. Key Idea: Even after rotation, one half of the array is always sorted. By identifying the sorted half and checking if the target lies within it, we can eliminate half of the search space in every step. => Time Complexity: O(log n) => Space Complexity: O(1) Great problem to strengthen binary search fundamentals and edge-case handling. #BinarySearch #Array #DivideAndConquer #LeetCode #DSA #InterviewPreparation #Java
To view or add a comment, sign in
-
-
✅ Day 83 of #100DaysOfLeetCode Problem: Next Permutation Difficulty: Medium Key Insight: To find the next permutation, we must generate the next lexicographically greater arrangement. The trick is to modify the array from the right side, where changes affect the order minimally. Approach: 1️⃣ Traverse from right → find the first index pivot where nums[i] < nums[i+1]. 👉 This marks the point where the next permutation can be formed. 2️⃣ If no pivot exists: 👉 The array is in descending order → reverse it to get the smallest permutation. 3️⃣ Otherwise: Find the element just greater than the pivot (search from the right). Swap them. Reverse the suffix after the pivot to make it the smallest possible. Complexity: Time: O(n) Space: O(1) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
Day - 61 Subsets II The problem - Given array that may contain duplicates, return all possible unique subsets. Example : candidates = [10,1,2,7,6,1,5], target = 8 → [[1,1,6],[1,2,5],[1,7],[2,6]] Brute Force - Generate all combinations iteratively, still exponential, but less elegant than recursion. Approach Used - •) Sort the nums array.nums.lengthcktrack(0, nums, subset, res). •) Base case, if i == nums.length, reached end, add new ArrayList(subset) to res and return. •) Recursive First case, include nums[i], add to subset, recurse with i+1, backtrack (remove). •) Recursive Second case, exclude nums[i], recurse with i+1 (don't add). Complexity - Time - O(2^n), each element include/exclude. Space - O(n), recursion depth. Note - Sort array. After excluding an element, skip all duplicates before next recursion to avoid duplicate subsets. #DSA #Java #SoftwareEngineering #InterviewPrep #LearnToCode #CodeDaily #ProblemSolving
To view or add a comment, sign in
-
-
Day - 57 Count Good Numbers The problem - A digit string is good if the digits at even indices are even (0,2,4,6,8) and digits at odd indices are prime (2,3,5,7). Return count of good digit strings of length n. Example : n = 1 → 5, n = 4 → 400 Brute Force - Generate all strings, but complexity would be exponential, and it is impractical to do for large numbers. Approach Used - •) Calculate even_places = (n+1)/2, odd_places = n/2. •)Use binary exponentiation, result = (5^even_places × 4^odd_places) % MOD10⁹+7. •) Binary exponentiation helper - squares base repeatedly, multiplies result when exp bit is 1. Complexity - Time - O(log n),binary exponential. Space - O(1), no extra space. Note - Even positions have 5 choices (0,2,4,6,8), odd positions have 4 choices (2,3,5,7). Answer = 5^(even_positions) × 4^(odd_positions) mod 10⁹+7. Use fast exponentiation. #DSA #Java #SoftwareEngineering #InterviewPrep #LearnToCode #CodeDaily #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 1 / 100 | Search a 2D Array. ->Today’s problem helped me understand how a 2D matrix can be treated like a 1D sorted array to apply Binary Search efficiently. •Problem Solved: 74. Search a 2D Matrix. 📚Topics: Binary Search, 2D array indexing. 🔑 Key Insight: By mapping a single index to (row, col) using division and modulo, we can apply Binary Search efficiently without extra space. #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 6 of #100DaysOfCode Solved Find the Duplicate Number on LeetCode using an in-place cyclic sort approach 🔁 🧠 Key insight: When numbers are in the range 1…n, each value belongs at index value − 1. If during placement we find that a number is already at its correct position, that number must be the duplicate. ⚙️ Approach: 🔹Iterate through the array 🔹Place each number at its correct index (num − 1) using swaps 🔹If a conflict occurs (same number already present), return it ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) (no extra data structures) #100DaysOfCode #LeetCode #DSA #CyclicSort #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
Day 10/100 Q 12 – Remove Duplicates from Sorted Array (DSA) Today’s problem was all about in-place array manipulation and two-pointer technique. 📌 Problem Statement Given a sorted integer array, remove duplicates in-place so that each unique element appears only once and return the count of unique elements. 🧠 Key Insight Since the array is already sorted, duplicates appear next to each other. Using two pointers, we can overwrite duplicates and keep all unique elements at the beginning of the array without using extra space. ⚙️ Approach Use one pointer to track the last unique element Traverse the array with another pointer Update the array when a new unique element is found ⏱ Complexity Time: O(n) Space: O(1) 💡 What I Learned How sorting simplifies duplicate problems Practical use of the two-pointer technique Writing space-optimized solutions 🔁 On to the next challenge! Consistency > Motivation 💪 #100DaysOfDSA #DSA #Arrays #TwoPointers #Java #ProblemSolving #LeetCode #CodingJourney #DailyLearning
To view or add a comment, sign in
-
Explore related topics
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