#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
How to delete all occurrences of a value in a doubly linked list
More Relevant Posts
-
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
-
-
📌 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
-
-
#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
To view or add a comment, sign in
-
-
Day 53/100 Problem Statement : Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number. Return the decimal value of the number in the linked list. The most significant bit is at the head of the linked list. Input: head = [1,0,1] Output: 5 Solution : https://lnkd.in/g3vuQeUx public int getDecimalValue(ListNode head) { ListNode temp=head; String s=""; while(temp!=null){ s+=temp.val; temp=temp.next; } int ans=Integer.parseInt(s,2); return ans; } #100DaysDSA #100DaysOfCode #Java #Leetcode #Neetcode #Neetcode250 #TUF
To view or add a comment, sign in
-
#100DaysOfCode – Day 87 Linked List Sorting & Node Rearrangement Linked List manipulation specifically, sorting a list that contains only 0s, 1s, and 2s. Problem: Given the head of a linked list containing only 0s, 1s, and 2s, rearrange the list so that all 0s appear first, followed by all 1s, and then all 2s. Example: Input: 1 → 2 → 2 → 1 → 0 → 2 Output: 0 → 1 → 1 → 2 → 2 → 2 Created three separate dummy linked lists to store 0s, 1s, and 2s. Traversed the original list once, linking each node to its respective list. Finally, merged all three lists in order (0s → 1s → 2s). Concepts Used: Linked List traversal Pointer manipulation Dummy node technique Time Complexity: O(N) Space Complexity: O(1) Efficient linked list problems often come down to clean pointer handling and a solid understanding of list connections no extra sorting needed! #takeUforward #100DaysOfCode #Java #LinkedList #GeeksForGeeks #ProblemSolving #DSA #CodingChallenge #CodeNewbie
To view or add a comment, sign in
-
-
#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 72 Rotate String Task: Given two strings s and goal, check if s can become goal after some number of shifts. Example: Input: s = "abcde", goal = "cdeab" → Output: true My Approach: Checked if both strings have the same length. Concatenated the original string: s + s. Verified if goal exists as a substring in the doubled string. Time Complexity: O(N) Space Complexity: O(N) Sometimes, the simplest observation can make a problem effortless like checking if goal exists inside s + s. #takeUforward #100DaysOfCode #Java #ProblemSolving #LeetCode #GeeksForGeeks #StringManipulation #CodeNewbie #DSA #CodingJourney
To view or add a comment, sign in
-
-
Day 50 of #75DaysDSAChallenge Problem: 7. Reverse Integer Difficulty: 🟠 Medium Platform: LeetCode 🧩 Problem Statement Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes it to go outside the signed 32-bit integer range [-2³¹, 2³¹ - 1], return 0. Example: Input: x = 123 Output: 321 💡 Approach 1️⃣ Extract the last digit using x % 10. 2️⃣ Remove the last digit using x / 10. 3️⃣ Add the digit to the reversed number. 4️⃣ Before updating, check for overflow conditions. 5️⃣ Continue until all digits are processed. #LeetCode #Java #DSA #CodingChallenge #75DaysDSAChallenge #ProblemSolving #TechLearning #CodingJourney
To view or add a comment, sign in
-
-
Day 54/100 – #100DaysOfCode 🚀 | #Java #SlidingWindow #TwoPointers ✅ Problem Solved: Count the Number of Substrings With Dominant Ones (LeetCode) 🧩 Problem Summary: You’re given a binary string. A substring is dominant if: number of 1s > number of 0s × k (where k is given). Return the total count of such substrings. 💡 Approach Used: Used an optimized Sliding Window + Two Pointers approach: Traverse with a right pointer. Adjust the left pointer whenever the substring stops being dominant. All valid windows contribute (left + 1) substrings. This avoids brute-force O(N²) and makes the solution efficient. ⚙️ Time Complexity: O(N) 📦 Space Complexity: O(1) ✨ Takeaway: Sliding window transforms heavy substring problems into clean linear-time solutions. #Java #LeetCode #SlidingWindow #TwoPointers #ProblemSolving #CodingChallenge #100DaysOfCode
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