🚀 Day 46 of #100DaysOfCode – Striver’s DSA Sheet 🚀 ✅ Topic Covered Today: Implement Queue Using Linked List 💡 Lesson of the Day (Approach-Focused): 🔹 Understanding the Core Idea A queue follows FIFO (First In, First Out) order. Using a Linked List, we maintain dynamic memory and perform operations efficiently with two pointers: front → points to the first node rear → points to the last node This avoids the limitations of fixed-size arrays. 🔹 1️⃣ Enqueue (Insert at Rear) Approach: Create a new node If queue is empty → front = rear = newNode Else → attach newNode at rear.next and move rear forward 🧮 Time Complexity: O(1) 💾 Space Complexity: O(1) extra 🔹 2️⃣ Dequeue (Remove from Front) Approach: If empty → nothing to remove Return node at front Move front = front.next If queue becomes empty → rear = null 🧮 Time Complexity: O(1) 🔹 3️⃣ Peek Return front.data without removing it. 🧮 Time Complexity: O(1) 🔹 4️⃣ isEmpty Check: front == null 🧮 Time Complexity: O(1) ✨ Key Idea: A linked-list-based queue never overflows (unless memory is full) and supports constant-time insertions and deletions — perfect for dynamic data scenarios. 💭 Learning: Today’s implementation strengthened my understanding of pointers and dynamic memory. Building a queue with linked nodes is a great way to visualize how real queue structures work behind the scenes. #100DaysOfCode #DSA #StriversSheet #Java #LinkedList #Queue #ProblemSolving #CodingJourney #LogicBuilding #Consistency
Implementing Queue Using Linked List in #100DaysOfCode
More Relevant Posts
-
🔥 Day 121 of My DSA Challenge – Remove Duplicates from Sorted List 🔷 Problem : 83. Remove Duplicates from Sorted List 🔷 Goal : Given the head of a sorted linked list, remove all duplicate nodes so that each element appears only once, while maintaining the sorted order. 🔷 Key Insight : This problem is a great example of linked list traversal and pointer management. Because the list is already sorted, all duplicates appear consecutively — which makes it easy to detect and skip them in one pass. The challenge is to handle links carefully so that only unique nodes remain connected. 🔷 Approach : 1️⃣ Initialize a dummy node (with a distinct value) to simplify linking. 2️⃣ Use a tail pointer to build a new list containing only unique elements. 3️⃣ Traverse the list using head: If head.val is not equal to the last added node (tail.val), link it. Otherwise, skip the duplicate. 4️⃣ Update tail.next = null at the end to avoid leftover links. 5️⃣ Return dummy.next as the new head. Time Complexity: O(n) Space Complexity: O(1) This problem strengthens concepts of : ✅ Duplicate handling in linked lists ✅ Pointer-based traversal ✅ Clean in-place modification Sometimes, cleaning a data structure is just about connecting only what truly matters. ⚡ Every small pattern makes a big difference in problem-solving clarity. One more concept locked in 💻 #Day121 #DSA #100DaysOfCode #LeetCode #Java #LinkedList #ProblemSolving #Algorithms #DataStructures #CodingChallenge #DeveloperJourney #EngineerMindset #GrowEveryday
To view or add a comment, sign in
-
-
🔥 Day 120 of My DSA Challenge – Remove Linked List Elements 🔷 Problem : 203. Remove Linked List Elements 🔷 Goal : Remove all nodes from a linked list whose value equals a given val, and return the updated head. 🔷 Key Insight : This problem focuses on Linked List traversal and pointer manipulation, one of the most fundamental concepts in data structures. Directly removing nodes from a linked list can get tricky, especially when dealing with the head node itself. To simplify this, we use a dummy node technique — a smart approach to handle head removals gracefully. 🔷 Approach : 1️⃣ Create a dummy node (dummy) that points to the start of the list. 2️⃣ Use a pointer (tail) to build a new filtered list. 3️⃣ Traverse the original list using head: If head.val != val, link it to the new list. Otherwise, skip the node. 4️⃣ After traversal, ensure tail.next = null to avoid cycles. 5️⃣ Return dummy.next (new head). Time Complexity: O(n) Space Complexity: O(1) This problem reinforces : ✅ Pointer management ✅ Dummy node usage ✅ Clean and safe linked list manipulation When handling linked lists, think in pointers — not positions. Efficient traversal and clean linking make all the difference. ⚡ 120 days strong. One more concept mastered, one step closer to mastery. 🚀 #Day120 #DSA #100DaysOfCode #LeetCode #Java #LinkedList #Pointers #ProblemSolving #Algorithms #DataStructures #CodingChallenge #DeveloperJourney #EngineerMindset #LogicBuilding
To view or add a comment, sign in
-
-
🔥 Day 122 of My DSA Challenge – Delete Node in a Linked List 🔷 Problem : 237. Delete Node in a Linked List 🔷 Goal : Delete a given node from a singly linked list — but without having access to the head of the list. 🔷 Key Insight : This is one of those classic Linked List interview questions that tests conceptual understanding over coding complexity. Normally, to delete a node, we need access to its previous node (to change the next pointer). But here, we’re not given the head — only the node that must be deleted. So how do we “delete” it? By using a clever trick — 👉 Copy the value of the next node into the current node. 👉 Then skip the next node (effectively deleting it). 🔷 Approach : 1️⃣ Overwrite the current node’s value with the value of its next node. 2️⃣ Link the current node’s next pointer to node.next.next. 3️⃣ This effectively removes the “next” node from the chain, achieving deletion without needing the head reference. Time Complexity: O(1) Space Complexity: O(1) This problem beautifully demonstrates data overwriting and pointer manipulation in linked lists. Sometimes, deleting isn’t about removing — it’s about replacing smartly. ⚡ Concepts reinforced : ✅ In-place modification ✅ Node manipulation ✅ Understanding memory links in Linked Lists Every concept — from arrays to linked lists — builds a deeper sense of logic and problem-solving clarity. #Day122 #DSA #100DaysOfCode #LeetCode #Java #LinkedList #ProblemSolving #Algorithms #DataStructures #CodingChallenge #DeveloperJourney #EngineerMindset #GrowEveryday #Learning
To view or add a comment, sign in
-
-
🚀 Day 45 of #100DaysOfCode – Striver’s DSA Sheet 🚀 ✅ Topic Covered Today: Implement Queue Using Array 💡 Lesson of the Day (Approach-Focused): 🔹 Understanding the Core Idea A queue follows FIFO (First In, First Out) order. Using an array, we simulate this behavior by maintaining: front pointer (f) → points to the first element rear pointer (r) → points to the last inserted element size / capacity → fixed array length 🔹 1️⃣ Enqueue (Insert) Check if queue is full → rear == size - 1 Increment rear Insert element at arr[rear] 🧮 Time Complexity: O(1) 💾 Space Complexity: O(1) extra 🔹 2️⃣ Dequeue (Remove) Check if queue is empty → front == -1 Return element at arr[front] Move front forward If queue becomes empty again → reset front = rear = -1 🧮 Time Complexity: O(1) 🔹 3️⃣ Peek Return arr[front] without removing it. 🧮 Time Complexity: O(1) 🔹 4️⃣ isEmpty / isFull isEmpty: front == -1 isFull: rear == size - 1 ✨ Key Idea: Implementing a queue using an array helps understand pointer movement, memory management, and why sometimes a circular queue is needed when fixed arrays cause unused space. 💭 Learning: Today strengthened my understanding of how queues work under the hood and how core operations actually use pointer updates — a great foundation before moving to circular queues and deque structures. #100DaysOfCode #DSA #StriversSheet #Java #Queue #Arrays #ProblemSolving #CodingJourney #LogicBuilding #Consistency
To view or add a comment, sign in
-
-
🔗 Day 05/10 of Linked List Series: Inserting a Node in the Middle of a Linked List 🧩 Let’s understand step-by-step how we can insert a new node at a specific position in a linked list 👇 ⚙️ Algorithm: Create a new node with the given data. Start from the head (first node). Move forward through the list until you reach the (index - 1) position — this is the node after which we’ll insert the new one. If the current node becomes null before reaching that position, it means the index is out of range → just return the original list. Otherwise: Point the new node’s next to the current node’s next. Then, make the current node’s next point to the new node. Finally, return the head (since the starting node never changes). 🧠 Example Walkthrough: Consider this list: 10 → 20 → 30 → 40 → 50 We want to insert 555 at index 2 (0-based index assumed). Steps: Traverse until index 1 (node with value 20) Insert new node after 20 Final list becomes: 10 → 20 → 555 → 30 → 40 → 50 💡 Think of it like inserting a new coach in the middle of a train — you disconnect one link, add the new one, and reconnect it! 📚 Keep following this series to master Linked Lists step by step! #CodeWithLakkojuEswaraSai #CodeWithLakkojuEswaraSai_LinkedList #Java #DSA #ProblemSolving #JavaFullStack #10000Coders
To view or add a comment, sign in
-
-
🚀 Just solved LeetCode #105 – Construct Binary Tree from Preorder and Inorder Traversal 🌳✅ 🧩 Problem: Given two integer arrays, preorder and inorder, where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, the goal is to construct and return the binary tree. 💡 My Approach: I used recursion to rebuild the tree based on the properties of preorder and inorder traversals. 1️⃣ The first element of the preorder array is always the root of the tree. 2️⃣ Find that root in the inorder array — elements to the left form the left subtree, and elements to the right form the right subtree. 3️⃣ Recursively build the left and right subtrees using the corresponding segments of preorder and inorder arrays. 4️⃣ Return the constructed root node once the recursion completes. This approach efficiently utilizes the traversal properties to reconstruct the binary tree from scratch. 🌲 📘 What I Learned: 🌱 Strengthened my understanding of how preorder and inorder traversals relate to each other. 🧠 Gained deeper insight into recursive problem-solving and subarray management. 🔁 Improved my ability to think recursively and break down problems into smaller components. 💻 Appreciated how recursion beautifully models hierarchical data structures like trees. #LeetCode #Java #ProblemSolving #CodingJourney #DataStructures #BinaryTree
To view or add a comment, sign in
-
-
📌 Day 17/100 - Design HashSet (LeetCode 705) 🔹 Problem: Design a HashSet without using built-in hashing libraries. Support add(key), remove(key), and contains(key) efficiently. 🔹 Approach: Used a boolean array where the index directly maps to the key. This gives O(1) access to mark presence or absence: add(key) → arr[key] = true remove(key) → arr[key] = false contains(key) → return arr[key] 🔹 Time Complexity: O(1) for add, remove, and contains 🔹 Space Complexity: O(N), where N is the range of possible keys (e.g., 1,000,001) 🔹 Key Learnings: Simple data representations can be the most performant for bounded key ranges. Always consider time vs memory trade-offs when designing data structures. Recreating core data structures reinforces how built-ins work under the hood. #100DaysOfCode #LeetCode #Java #DSA #ProblemSolving #HashSet #CodingJourney
To view or add a comment, sign in
-
-
✅ Day 75 of LeetCode Medium/Hard Edition Today’s challenge was “Range Add Queries 2D” — a fascinating problem that tests your understanding of grid manipulation, optimization, and prefix-sum based range updates ⚡💡 📦 Problem: You're given an n × n matrix initialized with zeros. Each query specifies a rectangle (r1, c1) to (r2, c2), and you must add +1 to every cell inside this rectangle. Return the final matrix after applying all queries. 🔗 Problem Link: https://lnkd.in/g9ct4VtM ✅ My Submission: https://lnkd.in/gkSe-sP8 💡 Thought Process: A direct brute-force update for each query results in O(q × n²) complexity — too slow for large constraints. The key insight is to replace repeated cell updates with a 2D Difference Array, which allows you to update an entire rectangle using only four operations: Add +1 at the start point Subtract at the boundaries Propagate values through prefix sums After building the difference matrix, we use: Row-wise prefix sums Column-wise prefix sums This reconstructs the final grid efficiently. This method transforms the problem from potentially billions of operations into a neat and fast O(q + n²) solution. 🎯 How the Difference Array Helps: Avoids updating each cell individually Captures changes in a compact form Spreads values using cumulative sums Works perfectly for rectangular range additions ⚙️ Complexity: ⏱ Time: O(q + n²) with prefix sums 💾 Space: O(n²) for the difference matrix #LeetCodeMediumHardEdition #100DaysChallenge #PrefixSum #Optimization #Java #DSA #ProblemSolving #LearnEveryday
To view or add a comment, sign in
-
-
🌟 Day 39 of #100DaysOfCode 🌟 🔍 Exploring String Simplification — Removing Duplicates with Precision 🔹 What I Solved Today’s challenge was about simplifying strings by repeatedly removing adjacent duplicate characters until no more exist. A great exercise in stack logic and string manipulation efficiency — proving that small, precise steps can clean up even the messiest data. 🧩 Problem: Remove All Adjacent Duplicates in String 💡 Problem Statement Given a string s, repeatedly remove two adjacent equal letters until none remain. Return the final string after all such duplicate removals. 💡 Example 1: Input: "abbaca" → Output: "ca" Explanation: Remove "bb" → "aaca" → remove "aa" → "ca" 💡 Example 2: Input: "azxxzy" → Output: "ay" 🧠 Concepts Used Stack-based String Processing Iteration & Character Comparison Optimized Linear Solution → O(n) Time, O(n) Space ⚙️ Approach 1️⃣ Use a StringBuilder as a stack. 2️⃣ Traverse each character: • If the current char equals the top of the stack → pop it. • Else → push it. 3️⃣ Return the resulting string from the stack. 🚀 What I Learned ✨ Stack patterns shine even in string problems. ✨ Clean, efficient logic leads to readable and reliable solutions. ✨ Removing redundancy — in code or mindset — leads to clarity. 💬 Reflection Today’s lesson reminded me that refining logic is just like refining thoughts — remove the noise, and the solution becomes clear. #100DaysOfCode #Day39 #StringManipulation #RemoveDuplicates #Java #CodingJourney
To view or add a comment, sign in
-
-
🚀 Today’s DSA Challenge: Vertical Order Traversal of a Binary Tree (LeetCode 987) This problem was quite interesting and taught me how powerful combinations of data structures can be when used smartly. The goal: Given a binary tree, we need to return its vertical order traversal — where nodes are grouped by their column index (left to right), and for each column, nodes are sorted first by row, then by their value. Here’s how I approached it 👇 🔹 I used a TreeMap<Integer, TreeMap<Integer, PriorityQueue<Integer>>> to store nodes in this structure: column -> row -> minHeap of node values This ensures everything stays sorted automatically (both columns and rows). 🔹 I performed a BFS traversal using a Queue<Tuple> where each Tuple stores (node, row, col) coordinates. Whenever I go left, column decreases by 1; when I go right, it increases by 1. 🔹 After traversing the entire tree, I built the final list column by column, row by row. This problem really strengthened my understanding of: ✅ BFS traversal on trees ✅ Nested TreeMaps for maintaining sorted ordering ✅ PriorityQueue usage to resolve same-row value ordering ➡️ Time & Space Complexity Analysis Time Complexity: O(N log N) 👉 Each node is visited once during BFS (O(N)), and every insertion and retrieval in the nested TreeMap + PriorityQueue takes O(log N) time. Space Complexity: O(N) 👉 Because we store all nodes in the map, queue, and result list during traversal. #Java #DSA #LeetCode #BinaryTree #LearningJourney #Coding #Software Engineer #InterviewPreparation #Problem Solving
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