#100DaysOfCode – Day 80 Reverse Linked List Problem: Given the head of a singly linked list, reverse the list and return the reversed version. Example: Input: [1, 2, 3, 4, 5] Output: [5, 4, 3, 2, 1] My Approach: Used an iterative method to reverse the list efficiently by manipulating pointers. Initialized three pointers prev, temp, and front. Iteratively reversed each node’s direction until the end of the list was reached. Returned prev as the new head of the reversed list. Time Complexity: O(N) Space Complexity: O(1) Reversing a linked list might look tricky at first, but once you understand pointer manipulation it’s pure logic and flow. #takeUforward #100DaysOfCode #DSA #Java #ProblemSolving #LeetCode #LinkedList #Pointers #CodeNewbie
Kanhaiya Lal’s Post
More Relevant Posts
-
#100DaysOfCode – Day 85 Delete the Middle Node of a Linked List Problem Statement: Given the head of a singly linked list, delete the middle node and return the modified list. My Approach: Used two-pointer technique (slow & fast) to identify the middle node in a single traversal. Maintained a prev pointer to skip the middle node without extra space. Carefully handled the edge case when the list has only one node. Complexity: Time: O(N) Space: O(1) Linked lists are all about pointers and precision a small mistake (like an extra semicolon ";") can make a huge difference. Understanding how slow and fast pointers move gives deep insight into efficient traversal patterns. #LeetCode #100DaysOfCode #Java #LinkedList #ProblemSolving #takeUforward #CodingJourney #DataStructures #CodeNewbie
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 91 Delete All Occurrences in a Doubly Linked List Given a doubly linked list and a key x, the task is to remove every node whose value equals x, and return the updated list. Example: Input: 2 <-> 2 <-> 10 <-> 8 <-> 4 <-> 2 <-> 5 <-> 2 Key: 2 Output: 10 <-> 8 <-> 4 <-> 5 My Approach Traversed the list using a pointer temp. For every node where temp.data == x: If it’s the head, shifted the head forward. Otherwise, re-linked the previous and next nodes to bypass the current one. Continued until the end of the list. Time Complexity: O(N) Space Complexity: O(1) Working with doubly linked lists reinforces how important pointer handling is. A single wrong link can break the entire structure but clean logic leads to elegant solutions! #100DaysOfCode #Java #DSA #LinkedList #GeeksforGeeks #ProblemSolving #CodingJourney #TakeUForward #CodeNewbie #LearningEveryday
To view or add a comment, sign in
-
-
📌 Day 29/100 – Partition List (LeetCode 86) 🔹 Problem: Given the head of a linked list and a value x, rearrange the list so that: All nodes less than x come first Followed by nodes greater than or equal to x While preserving the original order within each group 🔹 Approach: I used the two-list technique: One list for nodes < x One list for nodes ≥ x Traverse once and attach nodes accordingly Finally, connect both lists This approach keeps the ordering intact and ensures an efficient O(n) solution. 🔹 Key Learnings: Dummy nodes simplify linked list manipulations a lot Maintaining order inside partitions needs careful pointer handling Merging lists becomes easy when structure is clear #100DaysOfCode #Day29 #LeetCode #Java #DSA #LinkedList #ProblemSolving #CodingJourney #KeepLearning
To view or add a comment, sign in
-
-
Day 29/100 – #100DaysOfCode 🚀 | #Java #LeetCode #BinaryTree ✅ Problem Solved: Verify Preorder Serialization of a Binary Tree 🌲 🧩 Problem Summary: Given a string representing a preorder serialization of a binary tree, determine if it’s valid. Example: "9,3,4,#,#,1,#,#,2,#,6,#,#" → ✅ valid "1,#" → ❌ invalid 💡 Approach Used: Used the slot-counting method 🧠 Each node uses one slot and non-null nodes create two new slots. Process the preorder string, ensuring slots never go negative and exactly zero remain at the end. ⚙️ Time Complexity: O(n) 📦 Space Complexity: O(1) ✨ Takeaway: This problem teaches how binary tree structure can be validated with simple counting logic — no need to rebuild the tree! 🌱 #Java #LeetCode #BinaryTree #100DaysOfCode #ProblemSolving #CodingChallenge
To view or add a comment, sign in
-
-
💡 LeetCode #2824 — Count Pairs Whose Sum Is Less Than Target Today I solved LeetCode Problem 2824: Count Pairs Whose Sum Is Less Than Target 🔢 Problem Summary: Given an integer array nums and a number target, return the number of pairs (i, j) where i < j and nums[i] + nums[j] < target Key Idea: Use the two-pointer technique after sorting the array. Sort the array to make pair checking efficient. Keep one pointer at the start (i) and one at the end (j). If nums[i] + nums[j] is less than target, then all pairs between i and j are valid → add (j - i) to the count. Otherwise, move the right pointer left. This gives an O(n log n) solution due to sorting. #LeetCode #Java #DSA #TwoPointers #Sorting #ProblemSolving #CodingChallenge
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
-
-
🚀Day 29/100 - Problem of the day :- Minimum Number of Increments on Subarrays to Form a Target Array. 🎯 Goal: Solve the “Minimum Number of Increments on Subarrays to Form a Target Array” problem efficiently on LeetCode. 💡 Core Idea: The key observation is that we only need to add operations when the current element is greater than the previous one. So, the total operations = target[0] + sum(max(target[i] - target[i-1], 0)). This approach ensures we only count necessary increments, avoiding redundant steps. 🎯 Key Takeaway: By focusing on the difference between consecutive elements, we transform a complex problem into a simple linear pass — achieving both clarity and top performance. ✅ Passed all 129 test cases 🚀 Runtime beats 100% of Java submissions 💾 Space Complexity: O(1) — constant extra space used. ⏱ Time Complexity: O(n) — single pass through the array. #Java #DSA #ProblemSolving #Day29 #Leetcode #CodingJourney #100DaysChallenge
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 9 𝐨𝐟 𝐦𝐲 #100𝐃𝐚𝐲𝐬𝐎𝐟𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 𝐏𝐫𝐨𝐛𝐥𝐞𝐦𝐬 𝐒𝐨𝐥𝐯𝐞𝐝 𝐓𝐨𝐝𝐚𝐲 : 🔥 1️⃣ Leetcode (Medium) – Product of Array Except Self 💡 Key Idea: Instead of using division, use Prefix and Suffix Products. Prefix → product of all elements before the current index Suffix → product of all elements after the current index Multiply both to get the result for each element. 🧾 Time Complexity: O(n) ⚙️ Space Complexity: O(1) #DSA #Java #100DaysOfCode #ProblemSolving #LearningInPublic #SortingAlgorithms #CodingJourney
To view or add a comment, sign in
-
-
💡 Day 34/100 ✅ Remove Zeros in Decimal Representation Today’s challenge was about removing all the zeros from a given number’s decimal form. For example, 1020030 → 123. It might look simple, but it reinforced the importance of handling edge cases like n = 0 or n = 1000, which can lead to empty strings or parsing errors. 🧠 Key Features: Practiced both String-based and Math-based approaches. Explored the difference between using int and long for large numbers. Strengthened understanding of number-to-string conversions and parsing. ⚙️ Time Complexity: O(d) (where d = number of digits) 💾 Space Complexity: O(d) #Day33Of100 #DSA #Java #ProblemSolving #CodingJourney #100DaysOfCode #LearnEveryday #LogicBuilding
To view or add a comment, sign in
-
-
🚀 Day 7️⃣7️⃣ of #100DaysOfCode Solved LeetCode Problem #1437 — Check If All 1’s Are at Least K Places Away 🔍✨ 🔧 Concept: Given a binary array, ensure that every 1 is separated by at least K zeros. The solution simply tracks the previous index of 1 and checks the distance. 🧠 Key Idea: Scan the array once When a 1 is found, verify: 👉 currentIndex - previousIndex > k If any pair violates this, return false ⚡ Efficient: Time Complexity: O(n) Space Complexity: O(1) Another clean and efficient logical problem solved. #LeetCode #Java #Arrays #ProblemSolving #DSA #CodingJourney #100DaysOfCode #DeveloperLife #Motivation
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