Solved LeetCode #19: Remove Nth Node from End of List

📌 Day 35/150 – Remove Nth Node From End of List (LeetCode #19) Today’s problem was a classic linked list challenge that tested my understanding of two-pointer techniques — a powerful pattern for solving traversal-based problems efficiently. ⚙️ The task? Given the head of a linked list, remove the n-th node from the end and return the updated list. At first glance, it seems simple — just find the node from the end and delete it. But the twist lies in doing it in a single pass for optimal performance. 🚀 🔹 Brute Force Idea Traverse the list once to count total nodes, then again to remove the (length–n)th node. ✅ Easy to implement ❌ Requires two passes → not optimal 🔹 Optimal Approach – Two Pointer Technique To achieve this in one pass, we use two pointers: fast and slow. 👉 Move the fast pointer n steps ahead. 👉 Then move both fast and slow together until fast reaches the end. 👉 The slow pointer will be just before the target node — remove it cleanly. This elegant trick ensures we traverse the list only once! 🧠 Example: Input: head = [1, 2, 3, 4, 5], n = 2 Output: [1, 2, 3, 5] Here, the 2nd node from the end (4) is removed using the one-pass logic. ⏱️ Time Complexity: O(L) 📦 Space Complexity: O(1) 💡 Learning: Linked list problems often look tricky because of pointer manipulation, but patterns like slow & fast pointers simplify them beautifully. Recognizing and reusing such patterns across problems is a major step in mastering DSA. 💪 #150DaysOfCode #LeetCode #ProblemSolving #LinkedList #TwoPointer #CodingChallenge #SoftwareEngineering #DSA #Cplusplus #Developers #TechCommunity #LearningJourney 🚀

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories