🔥 Day 100 of #100DaysOfCode — FINAL DAY 🎯 Today’s problem: LeetCode – Reverse Linked List 🔗🔄 📌 Problem Summary You are given the head of a singly linked list. 👉 Reverse the list and return the new head. Example: [1,2,3,4,5] → [5,4,3,2,1] 🧠 Approach: Iterative (Two Pointers) We reverse the list by changing the direction of pointers. ⚙️ Logic We maintain three pointers: prev → previous node (initially null) curr → current node (starts at head) next → temporary pointer to store next node 💻 Code Insight ListNode prev = null; ListNode curr = head; while(curr != null){ ListNode temp = curr.next; curr.next = prev; prev = curr; curr = temp; } return prev; 💡 How It Works At each step: Reverse the link (curr.next = prev) Move both pointers forward Gradually, the entire list gets reversed. ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) 🚀 Performance Runtime: 0 ms Beats 100% submissions 🔥 Memory: 44.29 MB 🧠 Key Learning This is one of the most fundamental linked list problems. It teaches: Pointer manipulation In-place operations Iterative thinking 🎉 100 Days Completed! From: Arrays & Strings Sliding Window Stack & Queue Monotonic Stack Binary Search Linked Lists 👉 To building strong problem-solving skills 💪 Consistency > Motivation 🔥 🚀 What’s Next? Start Advanced DSA (Graphs / DP) Build real-world projects Contribute to Open Source (GSoC ready 💪) Practice mock interviews 💬 This journey is just the beginning. #100DaysOfCode #LeetCode #DSA #Java #CodingJourney #Consistency #InterviewPrep
Reverse Linked List in Java
More Relevant Posts
-
**Day 110 of #365DaysOfLeetCode Challenge** Today’s problem: **Add Two Numbers II (LeetCode 445)** A very elegant **Linked List + Stack** problem. We are given two numbers stored in linked lists where the **most significant digit comes first**. Example: `7243 + 564 = 7807` Lists: `[7,2,4,3] + [5,6,4] = [7,8,0,7]` 💡 **Core Challenge:** Normally addition starts from the **last digit**… But linked lists are given from the **front**. So how do we add from right to left **without reversing the lists**? 📌 **Approach:** 1️⃣ Push digits of both linked lists into stacks 2️⃣ Pop from stacks → gives digits from right to left 3️⃣ Add with carry 4️⃣ Insert new node at the front of result list This preserves correct order naturally ⚡ **Time Complexity:** O(n + m) ⚡ **Space Complexity:** O(n + m) **What I learned today:** Stacks are incredibly useful when you need to process data in **reverse order**. 💭 **Key Takeaway:** If you can’t move backward easily: 👉 Use a stack 👉 Simulate reverse traversal 👉 Build result from front Clean solution. Smart idea. Great interview problem #LeetCode #DSA #LinkedList #Stack #CodingChallenge #ProblemSolving #Java #TechJourney #Consistency
To view or add a comment, sign in
-
-
🔥 Day 99 of #100DaysOfCode Today’s problem: LeetCode – Merge Two Sorted Lists 🔗📊 📌 Problem Summary You are given two sorted linked lists. 👉 Merge them into one sorted linked list and return the head. Example: list1 = [1,2,4] list2 = [1,3,4] Output → [1,1,2,3,4,4] 🧠 Approach: Recursion We compare nodes from both lists and recursively build the merged list. ⚙️ Logic 1️⃣ Base cases: If list1 == null → return list2 If list2 == null → return list1 2️⃣ Compare current nodes: If list1.val <= list2.val → attach list1 and recurse Else → attach list2 and recurse 💻 Code Insight if(list1.val <= list2.val){ list1.next = mergeTwoLists(list1.next, list2); return list1; }else{ list2.next = mergeTwoLists(list1, list2.next); return list2; } 💡 Why Recursion Works? Each step: Picks the smaller node Reduces the problem size Eventually reaches base case → builds sorted list naturally. ⏱ Time Complexity: O(n + m) 💾 Space Complexity: O(n + m) (recursion stack) 🚀 Performance Runtime: 0 ms Beats 100% submissions 🔥 Memory: 44.23 MB 🧠 Key Learning This problem is fundamental for: Linked list manipulation Divide & conquer Merge sort logic 🚨 Almost There! Just 1 day left to complete the #100DaysOfCode challenge 🎯🔥 From basics → advanced patterns → consistency. On to the final Day 100 🚀 #100DaysOfCode #LeetCode #LinkedList #Recursion #Java #DSA #InterviewPrep
To view or add a comment, sign in
-
-
**Day 109 of #365DaysOfLeetCode Challenge** Today’s problem: **132 Pattern (LeetCode 456)** This problem looks tricky at first because it asks for a hidden subsequence: Find indices `i < j < k` such that: `nums[i] < nums[k] < nums[j]` That forms the **132 pattern** 💡 **Core Idea: Use a Monotonic Stack** Instead of checking all triplets (**O(n³)** ❌), we scan from **right to left**. Why reverse? Because while moving backward: * We try to build possible `3` values using a stack * Track the best possible `2` using a variable called `third` 👉 If we ever find a number smaller than `third`, then: `nums[i] < third < nums[j]` 📌 **Approach:** * Traverse from end to start * Maintain decreasing stack * Pop smaller elements and update `third` * If current number < `third` → return true ⚡ **Time Complexity:** O(n) ⚡ **Space Complexity:** O(n) **What I learned today:** Some array problems become much easier when traversed **backward instead of forward**. 💭 **Key Takeaway:** When the problem asks for hidden order relations: 👉 Think stacks 👉 Think reverse traversal 👉 Think maintaining candidates dynamically This was a great reminder that brute force is rarely the final answer #LeetCode #DSA #MonotonicStack #Arrays #CodingChallenge #ProblemSolving #Java #TechJourney #Consistency
To view or add a comment, sign in
-
-
🧠 LeetCode POTD — The Biggest Hint Was Hidden in One Line 1855. Maximum Distance Between a Pair of Values Today's problem looked tricky at first considering the time complexity. We need to find indices (i, j) such that: 👉 i <= j 👉 nums1[i] <= nums2[j] And maximize: 👉 j - i ━━━━━━━━━━━━━━━━━━━ My first instinct? Try brute force combinations. Check many pairs. Maybe binary search. But I was missing the most important line in the question: Both arrays are non-increasing (sorted). That one detail changes everything. ━━━━━━━━━━━━━━━━━━━ 💡 Once I noticed that, the solution became a clean two-pointer approach. Start with: 👉 i = 0 in nums1 👉 j = 0 in nums2 Then: If nums1[i] <= nums2[j] 👉 This pair is valid 👉 Update answer with j - i 👉 Move j forward to try for a bigger distance Else: 👉 Current i is too large 👉 Move i forward Because arrays are sorted, once a pair fails, moving i is the only useful move. ━━━━━━━━━━━━━━━━━━━ 📌 What I liked about this problem: The challenge wasn't the coding. It was reading carefully enough to spot the property that unlocks the solution. ✅ Sometimes the answer is already in the problem statement. ✅ You just have to notice it. Curious — did anyone else miss the sorted hint at first? 👀 #LeetCode #TwoPointers #ProblemSolving #SoftwareEngineering #DSA #Java #c++ #SDE
To view or add a comment, sign in
-
-
🚀 Day 17/30– DSA Challenge 📌 LeetCode Problem – Plus One 📝 Problem Statement You are given a large integer represented as an array of digits. Increment the integer by one and return the resulting array. 📌 Example Input: digits = [1,2,3] Output: [1,2,4] 📌 Edge Case Input: [9,9,9] Output: [1,0,0,0] 💡 Key Insight Addition starts from the last digit. 👉 If digit < 9 → just add 1 and return 👉 If digit == 9 → it becomes 0 and carry moves left If all digits are 9 → create a new array. 🚀 Algorithm 1️⃣ Traverse from right → left 2️⃣ If digit < 9: Increment it Return array 3️⃣ Else: Set digit = 0 Continue 4️⃣ If loop ends → all were 9 Create new array of size n+1 Set first element = 1 ✅ Java Code (Optimal O(n)) class Solution { public int[] plusOne(int[] digits) { for (int i = digits.length - 1; i >= 0; i--) { if (digits[i] < 9) { digits[i]++; return digits; } digits[i] = 0; } // All digits were 9 int[] result = new int[digits.length + 1]; result[0] = 1; return result; } } ⏱ Complexity Time Complexity: O(n) Space Complexity: O(1) (except edge case) 📚 Key Learnings – Day 17 ✔ Handle carry carefully ✔ Think from right to left ✔ Edge cases matter more than logic here ✔ Small problems test attention to detail Simple idea. Tricky edge case. Clean implementation. Day 17 completed. Consistency continues 💪🔥 #30DaysOfCode #DSA #Java #InterviewPreparation #ProblemSolving #CodingJourney #Arrays #LeetCode
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 31 Today’s focus: Binary Search for boundaries and square roots. Problems solved: • Sqrt(x) (LeetCode 69) • Search Insert Position (LeetCode 35) Concepts used: • Binary Search • Search space reduction • Boundary conditions Key takeaway: In Sqrt(x), the goal is to find the integer square root. Using binary search, we search in the range [1, x] and check mid * mid against x to narrow down the answer. This avoids linear iteration and achieves O(log n) time. In Search Insert Position, we use binary search to find either: • The exact position of the target, or • The correct index where it should be inserted The key idea is that when the target is not found, the final position of the left pointer gives the correct insertion index. These problems highlight how binary search is not just for finding elements, but also for determining positions and boundaries efficiently. Continuing to strengthen fundamentals and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 𝟐/ 𝟏𝟎𝟎 — 𝐋𝐞𝐧𝐠𝐭𝐡 𝐨𝐟 𝐋𝐚𝐬𝐭 𝐖𝐨𝐫𝐝 (𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝟓𝟖) Most people learn string manipulation by just chaining built-in methods. But today made one thing clear 👇 👉 Efficient parsing is about knowing exactly when to stop. 🧠 Today’s Breakthrough: From the "easy way" ➔ to optimized engineering thinking • Built-in .split() ➔ fast to write, but wastes memory on new arrays • Forward traversal ➔ works, but forces you to read the entire string • Backward traversal ➔ starts at the end, uses O(1) space • Early exit ➔ breaks the loop immediately, thinking like a pro Same result. Entirely different levels of performance. 💡 The core idea (Backward Parsing): 👉 Start at the end ➔ skip any trailing spaces 👉 Count characters ➔ track the length of the word 👉 Hit a space again ➔ break the loop immediately Simple rule. Massive impact. #DSA #Java #SoftwareEngineering #LeetCode #CodingJourney #InterviewPrep #LearnInPublic
To view or add a comment, sign in
-
-
🚀 Day 65 of #100DaysOfCode Solved 108. Convert Sorted Array to Binary Search Tree on LeetCode 🔗 🧠 Key Insight: To build a height-balanced BST from a sorted array: 👉 Always pick the middle element as root This ensures: Left half → left subtree Right half → right subtree Balanced structure 🔥 ⚙️ Approach (Divide & Conquer): 1️⃣ Choose middle index: 🔹 mid = start + (end - start) / 2 2️⃣ Create root node: 🔹 node = new TreeNode(nums[mid]) 3️⃣ Build left subtree: 🔹 node.left = build(start, mid - 1) 4️⃣ Build right subtree: 🔹 node.right = build(mid + 1, end) 5️⃣ Return root ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(log n) (recursion stack) #100DaysOfCode #LeetCode #DSA #BinaryTree #BST #DivideAndConquer #Recursion #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 77 — Slow & Fast Pointer (Middle of the Linked List) Another day, another clean application of the slow‑fast pointer pattern — this time without cycle detection, just finding the midpoint efficiently. 📌 Problem Solved: - LeetCode 876 – Middle of the Linked List 🧠 Key Learnings: 1️⃣ The Problem Given the head of a singly linked list, return the middle node. If there are two middle nodes (even length), return the second one. 2️⃣ Why Slow‑Fast Pointer Works Here - `slow` moves 1 step at a time. - `fast` moves 2 steps at a time. - When `fast` reaches the end (or `fast.next == null`), `slow` is exactly at the middle. - For even length, `fast` ends at `null` → `slow` points to the second middle node (perfect for this problem’s requirement). 3️⃣ The Code (Simple & Elegant) while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; 4️⃣ Why Not Just Count Then Traverse? - Brute force: count nodes → traverse again → O(n) but two passes. - Slow‑fast pointer achieves the same in one pass with O(1) extra space. 💡 Takeaway: Slow‑fast pointer isn’t only for cycle detection. It’s a versatile tool for: - Finding middle (this problem) - Detecting cycles (LeetCode 141/142) - Finding duplicate numbers (LeetCode 287) - Happy number detection (LeetCode 202) Each problem adds a new dimension to understanding the same pattern. No guilt about past breaks — just showing up, solving, and growing. #DSA #SlowFastPointer #MiddleOfLinkedList #LeetCode #CodingJourney #Revision #Java #ProblemSolving #Consistency #GrowthMindset #TechCommunity #LearningInPublic
To view or add a comment, sign in
-
🚀 Day 11 – #50DaysLeetCodeChallenge Today I solved the 3Sum problem. 📌 Problem Statement Given an integer array nums, return all the unique triplets [nums[i], nums[j], nums[k]] such that: i ≠ j ≠ k nums[i] + nums[j] + nums[k] = 0 ⚠️ The solution must not contain duplicate triplets. Example: Input: [-1,0,1,2,-1,-4] Output: [[-1,-1,2], [-1,0,1]] 💡 Approach I Used – Sorting + Two Pointers 1️⃣ Sort the array 2️⃣ Fix one element i 3️⃣ Use two pointers: left = i + 1 right = end of array 4️⃣ Check the sum: If sum == 0 → add triplet If sum < 0 → move left++ If sum > 0 → move right-- 5️⃣ Skip duplicates to avoid repeated triplets ⚙️ Key Idea Reduce the problem from 3Sum → 2Sum (using two pointers) Sorting helps in efficient traversal and duplicate handling 🧠 What I learned today ✔️ How sorting simplifies complex problems ✔️ Converting problems into smaller subproblems (3Sum → 2Sum) ✔️ Handling duplicates carefully From simple arrays → advanced pointer techniques 🚀 Consistency is making a difference! #LeetCode #Java #DSA #CodingChallenge #ProblemSolving #SoftwareEngineering
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