🚀 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
Partition Linked List by Value x
More Relevant Posts
-
🚀 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
-
-
LeetCode 24 — Swap Nodes in Pairs This problem gives the head of a linked list. The task is to swap every two adjacent nodes and return the head of the modified list. One important constraint is that the node values cannot be modified — only the nodes themselves can be rearranged. Example : Input : 1 → 2 → 3 → 4 Output : 2 → 1 → 4 → 3 Each pair of adjacent nodes is swapped while keeping the rest of the list structure intact. Approach used — Pairwise Pointer Manipulation A dummy node is placed before the head to simplify pointer operations, especially for the first pair. During traversal : - first represents the first node of the pair - second represents the second node of the pair For every pair : - The first node is connected to the node after the pair. - The second node is linked before the first node. - The previous node is connected to the new head of the pair. After swapping, the pointer moves forward to process the next pair. This process continues until fewer than two nodes remain. This approach swaps nodes in O(n) time with O(1) extra space. #leetcode #datastructures #linkedlist #java #problemSolving
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
-
-
LeetCode 237 — Delete Node in a Linked List Worked on a linked list problem that has an unusual constraint. The task is to delete a node from a singly linked list. But instead of receiving the head of the list, only the node that needs to be deleted is given. For example : 4 → 5 → 1 → 9 If the node 5 is given, the list should become : 4 → 1 → 9 The tricky part is that without access to the previous node, the usual deletion method cannot be used. Approach used :- Value Replacement - Copy the value of the next node into the current node. - Then update the pointer so the current node skips the next node. By doing this, the next node effectively disappears from the list while the structure stays intact. This problem is a good reminder that linked list questions often require thinking in terms of pointer manipulation rather than direct deletion. #leetcode #datastructures #linkedlist #java
To view or add a comment, sign in
-
-
Day 44 – Find Pivot Index Solved a problem to find the index where the sum of elements on the left equals the sum of elements on the right. Key Learnings: Calculating total sum and maintaining left sum during traversal Using a single pass approach to reduce time complexity Applying prefix sum logic to avoid repeated calculations #DSA #Java #Arrays #PrefixSum #ProblemSolving #CodingPractice
To view or add a comment, sign in
-
-
LeetCode 83 — Remove Duplicates from Sorted List This problem gives the head of a sorted linked list and asks to remove all duplicate values so that each element appears only once. Since the list is already sorted, duplicate values always appear next to each other. The task is simply to skip repeated nodes while keeping the original order of the list. Example : Input : 1 → 1 → 2 → 3 → 3 Output : 1 → 2 → 3 Approach used — Single Pass Pointer Traversal Because the list is sorted, the solution can be done in one traversal. Two pointers are used during traversal : - One pointer (a) keeps track of the last unique node. - Another pointer (b) scans the list forward. When both nodes have the same value, the duplicate node is skipped. When a new value appears, the unique node is connected to it. At the end, the last node’s next pointer is set to null to ensure the list ends correctly. This approach works in O(n) time and O(1) extra space. #leetcode #datastructures #linkedlist #java #problemSolving
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 36 / 100 | Insert Interval -Intuition: -This problem is based on interval merging with insertion. The Simple idea is to insert the new interval in the correct position and merge if overlapping occurs. If the current interval ends before the new interval starts, there is no overlap, so add it directly. If the current interval starts before or equal to the new interval end, they overlap. "So instead of adding separately, update the new interval by taking the minimum start and maximum end". Once merging is done, add the merged interval and then add the remaining intervals. -Approach: O(n) Traverse all intervals from left to right. First, add all intervals that come before the new interval (no overlap). Then merge all overlapping intervals by updating: new.start = min(new.start, current.start) new.end = max(new.end, current.end) Add the merged interval to result. Finally, add all remaining intervals that come after. -Complexity: Time Complexity: O(n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 40 / 100 | Contains Duplicate II -Intuition: -The goal is to check whether there exist two equal elements within a distance k. -Instead of checking every pair, we only check for duplicates inside a window of size k. -If a number repeats within this window, the condition |i - j| ≤ k is satisfied, then return true. -Approach: O(n) -Use a HashSet to maintain a sliding window of size k. -Traverse through the array. -If the current element already exists in the set, return true. -Add the current element to the set. -If the window size exceeds k, remove the element that goes out of range (nums[i - k]). -If traversal completes without finding duplicates, return false. -Complexity: Time Complexity: O(n) Space Complexity: O(k) #100DaysOfCode #Java #DSA #LeetCode #SlidingWindow
To view or add a comment, sign in
-
-
Optimizing Sorting Logic Using Bit Manipulation (Java) Today I solved a problem that required sorting integers based on the number of set bits in their binary representation. Instead of using a direct sorting approach, I implemented a custom comparator with a PriorityQueue and used Brian Kernighan’s algorithm to count set bits efficiently: n = n & (n - 1) This reduces time complexity for counting bits to O(number of set bits). 📊 Result: • 77/77 test cases passed • Runtime: 7ms • Beat 78% of submissions Key takeaway: Optimization is not just about solving — it’s about choosing the right approach and reducing unnecessary operations. Bit manipulation continues to be one of the most powerful tools in algorithm design. #DSA #Java #BitManipulation #LeetCode #ProblemSolving #SoftwareEngineering
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