#100DaysOfCode – Day 76 Insert a Node in a Doubly Linked List Problem: Given a position p and a value x, insert a new node with data x after the p-th node (0-based indexing) in a doubly linked list. Example: Input: p = 2, x = 6 Output: 2 <-> 4 <-> 5 <-> 6 My Approach: Traversed to the p-th node using a simple loop. Updated pointers of the prev and next nodes carefully to maintain bidirectional linkage. Handled edge cases for insertion at the end of the list. Time Complexity: O(N) Space Complexity: O(1) Understanding pointer manipulation in linked lists builds a strong base for advanced data structure problems like deletion, reversal, and flattening of linked lists. #takeUforward #100DaysOfCode #DSA #Java #ProblemSolving #GeeksForGeeks #LinkedList #Pointers #CodingChallenge #CodeNewbie #LearningEveryday
Inserting a Node in a Doubly Linked List: A Step-by-Step Guide
More Relevant Posts
-
#100DaysOfCode – Day 77 Delete in a Doubly Linked List Problem: Given a doubly linked list and an integer x, remove the node at position x (1-indexed) and return the head of the updated list. Example: Input: 1 <-> 2 <-> 3 <-> 4, x = 3 Output: 1 <-> 2 <-> 4 My Approach: 1️⃣ Handled edge cases deleting the head or an empty list. 2️⃣ Traversed to the node at position x. 3️⃣ Updated both prev and next pointers to unlink the node cleanly. Time Complexity: O(N) Space Complexity: O(1) Understanding pointer manipulation in linked lists builds the foundation for mastering advanced data structures. Every prev and next link matters! #100DaysOfCode #Java #ProblemSolving #DSA #GeeksforGeeks #LinkedList #Pointers #TakeUForward #CodeNewbie #CodingJourney
To view or add a comment, sign in
-
-
🚀 Just Solved LeetCode #102 — Binary Tree Level Order Traversal 📘 Problem: Given the root of a binary tree, return the level order traversal of its nodes' values — meaning, traverse the tree level by level from left to right. Example: Input → [3,9,20,null,null,15,7] Output → [[3], [9,20], [15,7]] 🧠 My Approach: I solved this using a recursive approach instead of the usual queue-based BFS. 1️⃣ First, I calculated the total number of levels in the tree using recursion. 2️⃣ Then, for each level, I called a helper function `nThLevel()` to collect all nodes at that level. 3️⃣ I stored each level’s nodes in a separate list and finally returned a list of lists as the result. This approach helped me deeply understand the relationship between recursion depth and tree levels. 💡 What I Learned: ✅ Different ways to perform level order traversal — BFS vs recursion. ✅ How to effectively use recursion to explore each tree level. ✅ Improved understanding of function calls, base conditions, and data storage in recursive algorithms. #LeetCode #Java #DSA #BinaryTree #CodingJourney
To view or add a comment, sign in
-
-
🚀 Just implemented a custom Vector<T> class in Java — a dynamic array structure that grows automatically as elements are added. It supports key operations like push_back, pop_back, front, back, clear, and more — all while practicing the concepts of generics, dynamic memory allocation, and data encapsulation. Rebuilding core data structures from scratch is a great way to understand how tools like ArrayList work internally. 💻 #Java #DSA #LearningByDoing #CodingJourney #DataStructures Nohit Singh,Shreyash Khedekar,Anshuman Singh,Arpit Jain
To view or add a comment, sign in
-
📌 LeetCode Day 60 — #106. Construct Binary Tree from Inorder and Postorder Traversal Problem Description: You are given two integer arrays inorder and postorder representing the inorder and postorder traversal of a binary tree. Your task is to reconstruct the original binary tree from these traversals. Approach: Identify Root: In postorder, the last element is always the root. Find Root Index in Inorder: Using a HashMap, find the position of the root in the inorder array to separate left and right subtrees. Recursive Construction: Build the left subtree from the left part of inorder and corresponding postorder range. Build the right subtree similarly from the right parts. Return Root: Recursively combine subtrees to reconstruct the full tree. Complexity Analysis: Time Complexity: O(n) — each node is processed once using the HashMap lookup. Space Complexity: O(n) — recursion stack + map for index lookup. Hashtags: #BinaryTree #Recursion #HashMap #LogicBuilding #LeetCode100Days #Day60 #Java #ProblemSolving
To view or add a comment, sign in
-
-
🎯 Day 82 of #100DaysOfCode – LeetCode 137: Single Number II Today’s challenge was about identifying the unique number in an array where every element appears three times except for one. 🧩 Problem Statement: Given an integer array nums, where every element appears exactly three times except for one, find that single element that appears only once. Example: Input: [2,2,3,2] Output: 3 💭 Approach 1: Using HashMap At first, I solved this using a HashMap to count the frequency of each number. Traverse the array and store each element’s occurrence. Return the element whose count equals 1. 🕒 Time Complexity: O(n) 💾 Space Complexity: O(n) 🚀 Key Takeaway: Sometimes, the best way to optimize is not by adding more data structures, but by understanding how data behaves at the bit level. #100DaysOfCode #Day82 #LeetCode #Java #ProblemSolving #DSA #CodingJourney #BitManipulation #KeepLearning
To view or add a comment, sign in
-
-
🗓 Day 7 / 100 – #100DaysOfLeetCode 🔢 Problem 2536: Increment Submatrices by One Today’s challenge involved processing multiple submatrix increment queries on an n x n matrix, initially filled with zeros. 🧠 My Approach Instead of updating every cell inside each submatrix (which would be too slow for up to 10⁴ queries), I used a row-wise difference array technique. For each query [r1, c1, r2, c2]: Increment prefix[row][c1] Decrement prefix[row][c2+1] (if within bounds) This allows efficient marking of increments. Later, prefix-summing each row reconstructs the final matrix. ⏱ Time Complexity O(q × n) where q = number of queries (We touch r2 - r1 + 1 rows per query) 💾 Space Complexity O(n²) for the prefix matrix #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
Day 20 – Variables & Primitive Data Types (Memory-Level Understanding) Today, I explored how variables and primitive data types work internally in Java — beyond just syntax. 🔹 Key Learnings: - Stack vs Heap Memory: - Stack stores primitive values and references. - Heap stores actual objects and instance data. Primitive Data Types: - int, byte, short, long → store integer values. - float (32-bit) and double (64-bit) → stored using IEEE 754 format (sign, exponent, mantissa). - char → 2 bytes (Unicode). boolean → true/false (JVM dependent). Float vs Double: - Float → ~7 digits precision - Double → ~15 digits precision - Variable Scope & Lifetime: - Local → within a method. - Instance → within an object. - Static → shared across all objects. 🔹 Takeaway: Understanding how Java handles variables in memory helps write more efficient and predictable code — especially when dealing with references and object lifetimes. #Java #LearningJourney #Day20 #Variables #DataTypes #MemoryManagement
To view or add a comment, sign in
-
🚀 #DSAinJava #Day92 #300DayChallenge Another productive day in the #TUFWinterArc with @takeUforward! ❄️ Today’s focus was on Binary Search in 2D Matrices — where logic meets geometry. 🔢✨ 🔥 Problems Covered: 1️⃣ Find the Row with Maximum Number of 1’s (Easy) 💡 A warm-up to matrix search — optimized row-wise scanning using binary search! 2️⃣ Search in a 2D Matrix (Medium) 🔍 Treat the 2D matrix as a sorted 1D array — perfect blend of indexing and logic. 3️⃣ Search in a Row and Column Wise Sorted Matrix (Medium) 🧭 Start from the top-right corner and eliminate one direction at a time — O(n + m) magic! 4️⃣ Find Peak Element in 2D Matrix (Hard) 🌋 Binary search extended into two dimensions — find the “peak” using mid-column logic. 5️⃣ Matrix Median (Hard) 📊 An advanced challenge — apply binary search on possible values instead of indices. ✅ Reinforces “search on answer” even in multidimensional data. 🧠 Key Learnings Today: ✅ Extended binary search logic beyond 1D arrays ✅ Learned to visualize search space in grids ✅ Strengthened problem-solving with row-column elimination strategy ✅ Built stronger intuition for complex search conditions Each matrix problem added a new layer of pattern recognition and analytical thinking. The binary mindset continues to evolve — one grid at a time! 💪 #TUFWinterArc #TakeUForward #Java #DSA #BinarySearch #Matrix #ProblemSolving #LeetCode #CodingJourney #StayConsistent #LearnEveryday
To view or add a comment, sign in
-
-
Day 40 of 160 Days of DSA 🚀 Problem: Search in a Strictly Sorted Matrix Today I learned how the type of matrix sorting completely changes the search strategy. In a strictly sorted matrix: Each row is sorted in increasing order And the first element of each row is greater than the last element of the previous row This means the matrix can be treated like one fully sorted array. So the efficient approach is: Binary Search on the last column to identify the correct row. Binary Search inside that row to find the target. This gives a time complexity of: O(logn+logm)≈O(log(n⋅m))O(\log n + \log m) \approx O(\log(n \cdot m))O(logn+logm)≈O(log(n⋅m))✨ Key Takeaway: Understanding how data is sorted helps us choose the optimal searching technique — one small detail can change the entire solution. #100DaysOfCode #geeksforgeeks #dsa #java #binarysearch #codingjourney #logicbuilding #learningEveryday
To view or add a comment, sign in
-
-
💻 Day 65 of #LeetCode100DaysChallenge Solved LeetCode 219: Contains Duplicate II — a smart problem involving hashing and sliding window logic. 🧩 Problem: Given an integer array nums and an integer k, return true if there exist two distinct indices i and j such that: nums[i] == nums[j], and |i - j| <= k. 💡 Approach — HashMap (Index Tracking): 1️⃣ Use a HashMap to store each number and its most recent index. 2️⃣ For every element nums[i]: If it’s already in the map and i - map.get(nums[i]) <= k, return true. Otherwise, update the index in the map. 3️⃣ If no pair found, return false. ⚙️ Complexity: Time: O(N) — single pass through the array. Space: O(N) — for storing indices in the map. ✨ Key Takeaways: ✅ Strengthened understanding of index-based distance checking. ✅ Efficiently applied hash maps for tracking element positions. ✅ Learned how to implement O(N) duplicate checks with window constraints. #LeetCode #100DaysOfCode #Java #HashMap #SlidingWindow #DSA #ProblemSolving #CodingChallenge #WomenInTech
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