🚀 LeetCode #19 — Remove Nth Node From End of List Linked List problem ✔️ 👉 Remove the Nth node from the end of a linked list 📌 Example: Input: [1,2,3,4,5], n = 2 Output: [1,2,3,5] 💡 Approach (intuitive): • First, calculate the length of the list • Find the (len - n)th node • Update its next pointer to skip the target node 🧠 Edge Case: If the head itself needs to be removed → return head.next ⏱ Time: O(n) 📦 Space: O(1) #DSA #LeetCode #Java #LinkedList #InterviewPrep #CodingJourney
Remove Nth Node from End of Linked List
More Relevant Posts
-
💡 Day 38 of LeetCode Problem Solved! 🔧 🌟643. Maximum Average Subarray I🌟 Task : • You are given an integer array nums consisting of n elements, and an integer k. • Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. • Any answer with a calculation error less than 10-5 will be accepted. Example 1: Input: nums = [1,12,-5,-6,50,3], k = 4 Output: 12.75000 Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75 Example 2: Input: nums = [5], k = 1 Output: 5.00000 #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfChallenge #CodingJourney #KeepGrowing
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 94/100 ✅ 𝐏𝐫𝐨𝐛𝐥𝐞𝐦: 𝐑𝐞𝐩𝐞𝐚𝐭𝐞𝐝 𝐒𝐮𝐛𝐬𝐭𝐫𝐢𝐧𝐠 𝐏𝐚𝐭𝐭𝐞𝐫𝐧 Today’s problem was about checking whether a string can be formed by repeating one of its substrings multiple times. 💡 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬: Try all possible substring lengths up to n/2 Only consider lengths that divide the string completely Build and compare repeated string with original Time Complexity: O(n²) in worst case 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Iterate over possible substring sizes If length divides string → repeat substring Compare with original string If match found → return true #Day94 #100DaysOfCode #LeetCode #Java #DSA #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 89/100 – 𝐈𝐬 𝐒𝐮𝐛𝐬𝐞𝐪𝐮𝐞𝐧𝐜𝐞 Today’s problem was 𝐈𝐬 𝐒𝐮𝐛𝐬𝐞𝐪𝐮𝐞𝐧𝐜𝐞 — a simple yet fundamental problem to understand two-pointer technique. 🔍 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: We can efficiently check if one string is a subsequence of another using two pointers. 💡 𝐂𝐨𝐫𝐞 𝐈𝐝𝐞𝐚: Traverse both strings Move pointer of s only when characters match Always move pointer of t 𝐖𝐡𝐲 𝐢𝐭 𝐰𝐨𝐫𝐤𝐬? We try to match characters of s in order within t without disturbing sequence. ⚡ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Initialize two pointers i (for s) and j (for t) If characters match → move both Else → move only j If i reaches end of s → subsequence exists ⏱️ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 📦 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(1) #Day89 #100DaysOfCode #Java #DSA #LeetCode #TwoPointers #CodingJourney
To view or add a comment, sign in
-
-
Day 63 - Odd Even Linked List Today’s problem was about rearranging nodes based on their positions. Approach: • Maintain two pointers → odd and even • Traverse and rearrange links in-place • Keep track of even head to attach later • Merge odd list with even list at the end Key insight: 👉 Focus on node positions, not node values. Time Complexity: O(n) Space Complexity: O(1) #Day63 #LeetCode #Java #LinkedList #CodingPractice #TechJourney #DSA
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟔𝟗/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐅𝐢𝐧𝐝 𝐭𝐡𝐞 𝐒𝐭𝐫𝐢𝐧𝐠 𝐰𝐢𝐭𝐡 𝐋𝐂𝐏 Continuing my 𝟑𝟔𝟓 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 journey with a focus on 𝐩𝐫𝐨𝐛𝐥𝐞𝐦-𝐬𝐨𝐥𝐯𝐢𝐧𝐠, 𝐃𝐒𝐀, 𝐚𝐧𝐝 𝐜𝐨𝐧𝐬𝐢𝐬𝐭𝐞𝐧𝐜𝐲. 💪 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Construct the string greedily by assigning characters. If lcp[i][j] > 0, both positions must have the same character. After constructing the string, validate it by checking whether the LCP values match the expected recurrence. 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦 𝐮𝐬𝐞𝐝: Greedy construction + validation using LCP properties. ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧²) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 📈 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: When constructing strings from constraints, always validate the result using the original conditions to ensure correctness. #LeetCode #LeetCodeDaily #365DaysOfCode #DSA #Java #Greedy #Strings #ProblemSolving #LearningInPublic 👨💻 🔗 Problem link in comments 👇
To view or add a comment, sign in
-
-
Day 57 — LeetCode Progress (Java) Problem: Jump Game Required: Given an array where each element represents the maximum jump length at that position, determine if you can reach the last index. Idea: Track the farthest index reachable at every step. If at any point your current index exceeds this range, you’re stuck. Approach: Maintain a variable maxReach to store the farthest reachable index. Traverse the array: If current index > maxReach, return false (cannot proceed). Update maxReach = max(maxReach, i + nums[i]). If traversal completes, return true. Time Complexity: O(n) Space Complexity: O(1) #LeetCode #DSA #Java #Greedy #Arrays #Algorithms #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 92/100 ✅ 𝐏𝐫𝐨𝐛𝐥𝐞𝐦: 𝐓𝐡𝐢𝐫𝐝 𝐌𝐚𝐱𝐢𝐦𝐮𝐦 𝐍𝐮𝐦𝐛𝐞𝐫 Today’s problem was about finding the third distinct maximum number in an array. If it doesn’t exist, we return the maximum number instead. 💡 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬: Maintain three variables to track top 3 distinct values Handle 𝐝𝐮𝐩𝐥𝐢𝐜𝐚𝐭𝐞𝐬carefully Use 𝐋𝐨𝐧𝐠.𝐌𝐈𝐍_𝐕𝐀𝐋𝐔𝐄 to avoid edge case issues Single pass solution → 𝐎(𝐧) 𝐭𝐢𝐦𝐞 𝐜𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Instead of sorting, we efficiently track max1, max2, and max3 while iterating through the array. This improves performance and avoids unnecessary computations. #Day92 #100DaysOfCode #LeetCode #Java #DSA #CodingJourney #ProblemSolving #TechGrowth
To view or add a comment, sign in
-
-
💡 Day 48 of LeetCode Problem Solved! 🔧 🌟128. Longest Consecutive Sequence🌟 🔗 Solution Code: https://lnkd.in/gyN5ZJBr Task: Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. You must write an algorithm that runs in O(n) time. Example 1: Input: nums = [100,4,200,1,3,2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. Example 2: Input: nums = [0,3,7,2,5,8,4,6,0,1] Output: 9 Example 3: Input: nums = [1,0,1,2] Output: 3 #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfChallenge #CodingJourney #KeepGrowing
To view or add a comment, sign in
-
-
Day 9 of #75DaysOfDSA Solved: Longest Subarray with Sum = K (Non-negative array) 🔹 Approach: Sliding Window (Two Pointers) Used a dynamic window to maintain sum and adjust size efficiently. 🔹 Key Idea Expand window → increase sum Shrink window when sum > k Update max length when sum == k 🔹 Why it works Since all elements are non-negative, sum increases/decreases predictably, making sliding window optimal. 🔹 Complexity Time: O(n) Space: O(1) 🔹 Takeaway Choose sliding window when no negative numbers are involved; otherwise, use prefix sum + hashmap. #DSA #SlidingWindow #Java #CodingChallenge #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 64 — LeetCode Practice 🚀 ✅ Problem Solved: Create Target Array in the Given Order 💡 What I learned today: • Understood how insertion at a specific index works in arrays • Learned the concept of shifting elements to the right • Practiced handling multiple insert operations step-by-step • Improved clarity on array manipulation and indexing 📊 Approach: • Traverse both nums and index arrays • For each element, shift elements to the right • Insert the number at the given index • Repeat until the array is formed ⏱ Time Complexity: O(n²) 📦 Space Complexity: O(n) Simple problem, but helped me understand how arrays work internally during insert operations 💪✨ #Day64 #LeetCode #DSA #Java #LeetCodePractice #Consistency
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