🔥 Day 80 of my 100 Days of Code Problem: Remove Linked List Elements (LeetCode #203) Problem Statement (Simplified): Remove all nodes of a linked list that have a given value. Code : class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummy = new ListNode(0); // Dummy node dummy.next = head; ListNode current = dummy; while (current.next != null) { if (current.next.val == val) { current.next = current.next.next; // Skip the node } else { current = current.next; // Move forward } } return dummy.next; } } Time Complexity: O(n) traverse the list once. Space Complexity: O(1) constant extra space (just dummy node). #Day80 #LeetCode #100DaysOfCode #Java
"Day 80: Remove Linked List Elements in Java"
More Relevant Posts
-
Day 27/100 Problem Statement: There is a singly-linked list head and we want to delete a node node in it. You are given the node to be deleted node. You will not be given access to the first node of head. All the values of the linked list are unique, and it is guaranteed that the given node node is not the last node in the linked list. Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean: The value of the given node should not exist in the linked list. The number of nodes in the linked list should decrease by one. All the values before node should be in the same order. All the values after node should be in the same order. Custom testing: For the input, you should provide the entire linked list head and the node to be given node. node should not be the last node of the list and should be an actual node in the list. We will build the linked list and pass the node to your function. The output will be the entire list after calling your function. Input: head = [4,5,1,9], node = 5 Output: [4,1,9] Solution: https://lnkd.in/gi8zCZUG public void deleteNode(ListNode node) { node.val = node.next.val; node.next = node.next.next; } #100DaysDSA #100DaysOfCode #Java #LeetCode #Neetcode #Neetcode250
To view or add a comment, sign in
-
🔥 Day 90 of my 100 Days of Code Problem: Set Mismatch (LeetCode #645) Problem Statement (Simplified): You are given an array representing numbers from 1 to n. One number is duplicated, and another is missing. Return the numbers [duplicate, missing]. Code : class Solution { public int[] findErrorNums(int[] nums) { int n = nums.length; int[] count = new int[n + 1]; int duplicate = -1, missing = -1; // Count occurrences for (int num : nums) { count[num]++; } // Identify duplicate and missing for (int i = 1; i <= n; i++) { if (count[i] == 2) duplicate = i; else if (count[i] == 0) missing = i; } return new int[]{duplicate, missing}; } } Time Complexity: O(n) Space Complexity: O(n) #Day90 #LeetCode #100DaysOfCode #Java #Arrays #Hashing #SetMismatch
To view or add a comment, sign in
-
-
Using streams with `java.time` makes generating, transforming, and querying a sequence of dates or times surprisingly concise, such as using `LocalDate.EPOCH.datesUntil(LocalDate.now()).map(...)`.
To view or add a comment, sign in
-
-
🔥 Day 97 of my 100 Days of Code Problem: Binary Tree Inorder Traversal (LeetCode #94) Problem Description: Given the root of a binary tree, return the inorder traversal of its nodes’ values. Inorder means visiting nodes in this order: Left -> Node -> Right Java Solution: class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode current = root; while (current != null || !stack.isEmpty()) { // Reach the leftmost node while (current != null) { stack.push(current); current = current.left; } // Process node current = stack.pop(); result.add(current.val); // Visit right subtree current = current.right; } return result; } } Time Complexity: O(n) Space Complexity: O(n) (for stack in the worst case) #Day97 #LeetCode #100DaysOfCode #Java #BinaryTree #DFS
To view or add a comment, sign in
-
-
🚀 Day 9 of 100 Days of LeetCode 🧩 Problem: #242. Valid Anagram 💻 Difficulty: Easy 🔍 Concept: Check whether two strings are anagrams — i.e., they contain the same characters in the same frequency, just rearranged. ⚙️ Approach Used: Used a frequency array of size 26 to count occurrences of each character in the first string. Decreased counts while scanning the second string. If all frequencies end up zero ➜ both strings are anagrams ✅ 🧠 Key Learnings: Practiced frequency array patterns for string problems. Learned to avoid sorting-based solutions for better time complexity (O(n) instead of O(n log n)). 💻 Code (Java): class Solution { public boolean isAnagram(String s, String t) { int[] freq = new int[26]; for (char c : s.toCharArray()) freq[c - 'a']++; for (char c : t.toCharArray()) freq[c - 'a']--; for (int f : freq) if (f != 0) return false; return true; } } 💬 Reflection: Small wins count too! Even the simplest problems help in strengthening your logical foundation and pattern recognition. #100DaysOfLeetCode #Day9 #ProblemSolving #Java #LeetCode #DSA #CodingChallenge #LearningEveryday
To view or add a comment, sign in
-
-
Comparable (java.lang.Comparable) • Used to define the natural/default ordering of a class. • Class itself implements Comparable and overrides compareTo(Object o). • Affects how objects are sorted when using collections like TreeSet or Collections.sort(). • Limitation: Only one natural ordering per class. Comparator (java.util.Comparator) • Used to define custom ordering outside the class. • Implemented in a separate class (or using lambda/anonymous class). • Useful when you want multiple different sorting orders. • Provides compare(Object o1, Object o2) method.
To view or add a comment, sign in
-
#73day of #100daychallengecode 🧩 LeetCode 2095 – Delete the Middle Node of a Linked List Today’s challenge was a clean and logical Linked List problem — deleting the middle node from a singly linked list. 🧠 Problem Given the head of a linked list, delete the middle node and return the modified list. If the list has only one node, return null. 💡 Key Idea 1️⃣ Use the slow and fast pointer technique: Move fast two steps at a time and slow one step. When fast reaches the end, slow will be at the middle node. 2️⃣ Keep a pointer prev to track the node before slow. 3️⃣ Skip the middle node by doing prev.next = slow.next. ⚡ Complexity Time: O(n) Space: O(1) --- ✅ A perfect example of how two-pointer techniques make linked list problems simple and efficient. Small details like handling single-node edge cases are key to passing all test cases! #LeetCode #Java #LinkedList #ProblemSolving #CodingInterview #100DaysOfCode
To view or add a comment, sign in
-
-
🫰When to Use the Builder Pattern in Java Builder pattern is perfect for: ✅ Complex objects with many fields ✅ Immutable objects ✅ Optional parameters ✅ Cleaner, readable object creation Avoid it for: ❌ Simple objects ❌ Performance-critical code ❌ Short-lived objects Pro tip: Builder makes your code readable, maintainable, and scalable, especially for DTOs and entities. #Java #BuilderPattern #CleanCode #BackendDevelopment #SoftwareEngineering
To view or add a comment, sign in
-
🔥 Day 95 of my 100 Days of Code Problem: Container With Most Water (LeetCode #11) Problem Description: You are given an integer array height of length n. Each element represents a vertical line on the x-axis. Find two lines that together with the x-axis form a container that holds the most water. Return the maximum amount of water that can be contained. Java Solution: class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; int maxWater = 0; while (left < right) { int h = Math.min(height[left], height[right]); int w = right - left; maxWater = Math.max(maxWater, h * w); // Move the pointer pointing to the shorter line if (height[left] < height[right]) { left++; } else { right--; } } return maxWater; } } Time Complexity: O(n) Space Complexity: O(1) #Day95 #LeetCode #100DaysOfCode #Java #TwoPointers #Array
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