#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
Linked List Sorting: 0s, 1s, 2s Rearrangement
More Relevant Posts
-
#48Day of #50DaysOfCoding Staircase Pattern Problem (Java) Today, I solved the “Staircase” problem from HackerRank using Java — a simple yet visually satisfying problem that focuses on nested loops and pattern generation. Concept Used: - Nested loops to print spaces and hash symbols. - Right-aligned pattern formation using conditional structure. Logic: For each row from 1 to n: - Print (n - i) spaces. - Then print i hash symbols (#). - Move to the next line after each row. Example: For n = 4, the output is: ``` # ## ### #### ``` Complexity: - Time Complexity: O(n²) (nested loops) - Space Complexity: O(1) This problem helped reinforce how loop control and formatting logic work together to create structured patterns in programming.
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
-
-
#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
-
-
💡 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-68) LeetCode #3228 – Maximum Number of Operations to Move Ones to the End (Java Edition) Tackled this neat string problem using a greedy approach in Java. The challenge? Move '1's to the end of the string under specific movement rules, maximizing the number of valid operations. 🧠 Core Idea: Track how many '0's we've seen and how many '1's we've already moved. Only move a '1' if there's enough '0's to justify it and the next character is also '1'. 💻 Java Strategy: Loop through the string Use counters to manage '0's and '1's Let me know how you'd tweak this or if you see a more optimal path! #Java #LeetCode #GreedyAlgorithm #StringProblems #DSA #CodingChallenge #LinkedInTech #ProblemSolving
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
-
-
🔥 #100DaysOfDSA — Day 30/100 Topic: Reverse an Array in Java 🔁 💡 What I Did Today: Today, I explored how to reverse an array manually using the two-pointer approach. Instead of relying on built-in methods, I learned how to swap elements from both ends until the array is completely reversed. 🧠 Logic Used: Initialize two pointers: start = 0 (beginning of array) end = numbers.length - 1 (end of array) While start < end: Swap numbers[start] and numbers[end] Move pointers inward → start++ and end-- 📊 Example: Input → {2, 4, 6, 8, 10} Output → {10, 8, 6, 4, 2} ⚙️ Time Complexity: O(n) — each element is swapped once. O(1) space — done in-place without using extra arrays. ✨ Takeaway: Learning to reverse an array manually helps strengthen the concept of pointers, loops, and in-place operations. Sometimes the simplest logic gives the biggest "aha!" moment 💡 #100DaysOfCode #Day30 #Java #DSA #Arrays #ProblemSolving #CodingJourney #LearnInPublic #DeveloperLife #CodeNewbie #LogicBuilding
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 46: Is Subsequence (LeetCode #392) 📌 Problem Statement: Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence is formed by deleting some (possibly none) characters from the original string without disturbing the relative order of the remaining characters. ✅ My Approach: I used a two-pointer technique — one pointer iterates through string t, and the other tracks progress through string s. Each time a matching character is found, the pointer for s moves forward. If we reach the end of s, it means all its characters appeared in sequence within t. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 2 ms (Beats 68.53%) Memory: 41.32 MB (Beats 87.28%) 💡 Reflection: A simple yet elegant problem that highlights how pointer movement can efficiently handle string comparisons without extra memory usage. ✨ #LeetCode #Java #Strings #TwoPointers #100DaysOfCode #Day46
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