🚀 Day 99/100 – DSA Challenge Problem Solved: LeetCode 1721 – Swapping Nodes in a Linked List (Medium) Problem: You are given the head of a linked list and an integer k. Your task is to swap the values of the k-th node from the beginning and the k-th node from the end (1-indexed). Approach: First, determine the total length of the linked list. Then locate the k-th node from the start and the k-th node from the end using traversal. Finally, swap their values — no need to change the actual node connections. ✅ Key Takeaway: By combining counting and two-pointer logic, you can handle linked list manipulations efficiently without touching the internal structure. #100DaysOfDSA #LeetCode #Java #CodingChallenge #ProblemSolving #Algorithms
Solved LeetCode 1721: Swapping Nodes in a Linked List
More Relevant Posts
-
💡 LeetCode #88 — Merge Sorted Array Today I solved LeetCode Problem 88: Merge Sorted Array 🧩 Problem Summary: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. All merging should happen in-place, meaning you can’t use extra space for another array. Key Idea: Use three pointers starting from the end: i → last valid element in nums1 j → last element in nums2 k → last index in nums1 (total length) Compare elements from the back and fill nums1 from the end — this avoids overwriting elements we haven’t processed yet. #LeetCode #Java #DSA #TwoPointers #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 115 days of #200DaysOfCode Problem: 24. Swap Nodes in Pairs Problem Statement: Given the head of a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes, only nodes themselves may be changed. Approach: Used a dummy node and iteratively swapped each adjacent node pair via pointer manipulation, which enabled in-place node swaps without extra space. Logic: Leveraged pointer rewiring to achieve the swaps efficiently with O(1) extra space and O(n) time complexity, cleanly iterating through the list to handle both even and odd-length cases. 👉 Question link 🔗: https://lnkd.in/g6cwvMgz #LeetCode #Java #LinkedList #Pointers #DSA #Coding #Algorithms #InterviewPrep #200DaysOfCode
To view or add a comment, sign in
-
-
💻 Day 34 — LeetCode 912: Sort an Array (Merge Sort Implementation) Today, I learned and implemented Merge Sort, a classic Divide and Conquer algorithm that efficiently sorts arrays with a time complexity of O(n log n). I applied it to LeetCode 912 (Sort an Array) — where the task is to sort an integer array without using built-in sorting methods. This problem helped me understand: How recursion splits the array into halves How merging combines sorted halves Why Merge Sort guarantees stable and consistent performance 🧠 Key takeaway: Merge Sort ensures O(n log n) in all cases (best, average, and worst), making it one of the most reliable sorting algorithms. #LeetCode #Day34 #DSA #SortingAlgorithms #MergeSort #CodingJourney #Java #ProblemSolving
To view or add a comment, sign in
-
-
📅 Day 82 of #100DaysOfLeetCode Problem: Delete Node in a BST (LeetCode #450) Approach: The goal is to delete a node with a specific key from a Binary Search Tree (BST). The deletion process involves two main steps: Search for the node to be deleted. Delete it while maintaining the BST property. There are three cases when deleting a node: Leaf node: Simply remove it. One child: Replace the node with its child. Two children: Find the inorder successor (smallest value in the right subtree), replace the node’s value with it, and delete the successor recursively. Complexity: ⏱️ Time: O(h), where h is the height of the BST. 💾 Space: O(h), recursive call stack. 🔗 Problem Link: https://lnkd.in/dx4XEUgz 🔗 Solution Link: https://lnkd.in/d3-RJ6yJ #LeetCode #100DaysOfCode #BinarySearchTree #Recursion #Java #DSA #Algorithms #ProblemSolving #CodeNewbie #TreeTraversal #StudyWithMe #DailyCoding #BuildInPublic #LearnToCode #CodingJourney
To view or add a comment, sign in
-
-
🚩 Problem: 349. Intersection of Two Arrays 🔥 Day 42 of #100DaysOfLeetCode 🔍 Problem Summary: Given two integer arrays nums1 and nums2, return an array of unique elements that appear in both arrays. The result must not contain duplicates. The order of elements in the output does not matter. ✅ Approach: Using HashSet Store elements of nums1 in a HashSet. Loop through nums2, and if an element exists in HashSet, add to result set. Convert the set into an array. 📊 Complexity: Time Complexity: O(m + n) Space Complexity: O(m + n) 🎯 Key Takeaway: This problem reinforces the concept of using HashSet to remove duplicates and perform efficient membership checks, which is fundamental for solving array and hashing problems optimally. Link:[https://lnkd.in/gz9Y99Ak] #100DaysOfLeetCode #Day42 #Problem349 #IntersectionOfArrays #HashSet #Java #DSA #Algorithms #CodingChallenge #ProblemSolving #LeetCode #InterviewPreparation #DataStructures #TechCareers #ArjunInfoSolution #CodingCommunity #CodeNewbie #ZeroToHero #SoftwareEngineering
To view or add a comment, sign in
-
-
✅Day 41 : Leetcode 153 - Find Minimum in Rotated Sorted Array #60DayOfLeetcodeChallenge 🧩 Problem Statement Given a sorted array that has been rotated at an unknown pivot, find the minimum element in the array. The array contains unique elements, and the solution must run in O(log n) time. 💡 My Approach I used a binary search technique to efficiently find the minimum element. I maintained two pointers, low and high. At each step, I calculated the mid-point. If the left part (nums[low] to nums[mid]) was sorted, I updated my answer with the smaller of nums[low] and current ans, and moved low to mid + 1. Otherwise, I updated my answer with nums[mid] and moved high to mid - 1. This approach ensures we keep narrowing the search space toward the minimum element. ⏱️ Time Complexity O(log n) — Because the search space is halved in each iteration. #BinarySearch #LeetCode #RotatedSortedArray #DSA #CodingPractice #Java #ProblemSolving
To view or add a comment, sign in
-
-
Day 54 of My DSA Challenge Problem: Count the number of pairs in an array whose sum equals a given target. Approach: Instead of the brute-force O(N²) method that checks every pair, I optimized the solution using a HashMap for frequency counting. Traverse the array once. For each element, check if (target - current element) exists in the map. If it does, it means those previous elements can form valid pairs with the current one. Update the frequency of the current element in the map. This smart use of a HashMap allows counting pairs in a single pass. Complexity: Time: O(N) Space: O(N) Key Insight: Using a frequency map turns an otherwise nested loop problem into a single-pass efficient solution — a common and elegant trick in array-based problems. #Day54 #DSAChallenge #HashMap #Optimization #ProblemSolving #Java #CodingChallenge #Algorithms #DataStructures #100DaysOfCode #GeeksforGeeks #LeetCode #CodingJourney #ProgrammersLife #TechLearning
To view or add a comment, sign in
-
-
✅ LeetCode Day 45 — Problem #162: Find Peak Element 🚀 Approach: I applied the Binary Search technique to locate a peak element efficiently. Initialize two pointers l and r. Compute the middle index m. If nums[m] < nums[m + 1], it means the peak lies on the right side → move l = m + 1. Otherwise, the peak lies on the left or at m → move r = m. Continue until l == r, which gives the index of the peak element. ⏱️ Time Complexity: O(log n) 💾 Space Complexity: O(1) 💡 Learning: Binary search can be applied beyond just sorted arrays — it’s a powerful pattern for problems with a “direction” property! #LeetCode #Day45 #DSA #BinarySearch #ProblemSolving #Java #100DaysOfCode #LearningEveryday #CodingChallenge
To view or add a comment, sign in
-
-
✅Day 44 : Leetcode 162 - Find Peak Element #60DayOfLeetcodeChallenge 🧩 Problem Statement: You are given a 0-indexed integer array nums. A peak element is an element that is strictly greater than its neighbors. Your task is to find a peak element and return its index. If the array contains multiple peaks, return the index of any one of them. You must solve this in O(log n) time complexity. 💡 My Approach: I used the Binary Search approach to solve this problem efficiently. Check for edge cases: If there’s only one element, return index 0. If the first element is greater than the second, it’s a peak → return 0. If the last element is greater than the second last, return n-1. Apply binary search between indices 1 and n-2: Find the middle index mid. If nums[mid] is greater than both its neighbors (nums[mid-1] and nums[mid+1]), we found the peak → return mid. If the left neighbor is greater, move the search to the left half. Otherwise, move to the right half. This guarantees logarithmic search efficiency. ⏱️ Time Complexity: O(log n) — due to binary search. 💾 Space Complexity: O(1) — constant extra space. #BinarySearch #LeetCode #FindPeakElement #DSA #Java #CodingPractice #ProblemSolving #LogN
To view or add a comment, sign in
-
-
NeetCode 27 | LeetCode #74 | Search in a 2D Matrix Flattened the 2D matrix logic into a binary search approach for efficient lookup. Treating rows and columns as a single sorted array — achieving O(log(m × n)) time complexity. A neat combination of logic and spatial understanding 🧩 #NeetCode #LeetCode #Java #DSA #BinarySearch #Algorithms #ProblemSolving #CodingJourney
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