
Remove Nth Node From End β The Smart Way to Solve in One Pass (LeetCode 19)
π Try the ProblemPractice here:https://leetcode.com/problems/remove-nth-node-from-end-of-list/π€ Letβs Think Differentlyβ¦Imagine this list:1 β 2 β 3 β 4 β 5You are asked:π Remove the 2nd node from the endSo counting from end:5 (1st), 4 (2nd) β remove thisFinal list:1 β 2 β 3 β 5π§ Problem in Simple WordsYou are given:Head of a linked listA number nπ Remove the nth node from the endπ Return the updated listπ¦ Constraints1 <= number of nodes <= 300 <= Node.val <= 1001 <= n <= size of listπ§© First Thought (Counting Method)π‘ IdeaCount total nodesFind position from start:position = total - nTraverse again and remove that nodeβ Code (Counting Approach)class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { if(head == null) return head; // Step 1: Count nodes int co = 0; ListNode tempHead = head; while(tempHead != null){ co++; tempHead = tempHead.next; } // Step 2: If removing head if(co == n) return head.next; // Step 3: Find node before target int k = co - n; int con = 1; ListNode temp = head; while(con < k){ temp = temp.next; con++; } // Step 4: Remove node temp.next = temp.next.next; return head; }}β±οΈ ComplexityTime ComplexityO(n) + O(n) = O(n)(two traversals)Space ComplexityO(1)β οΈ Limitation of This Approachπ It requires two passesBut the problem asks:Can you solve it in one pass?π Optimal Approach: Two Pointer Technique (One Pass)Now comes the interesting part π₯π§ Core IdeaWe use two pointers:fast pointerslow pointerπ― Trickπ Move fast pointer n steps aheadThen move both pointers together until:fast reaches endAt that moment:π slow will be at the node before the one to removeπ Why This WorksBecause the gap between fast and slow is always n nodesSo when fast reaches end:π slow is exactly where we need itπ₯ Step-by-Step VisualizationList:1 β 2 β 3 β 4 β 5n = 2Step 1: Move fast 2 stepsfast β 3slow β 1Step 2: Move both togetherfast β 4, slow β 2fast β 5, slow β 3fast β null, slow β 4π Now slow is at node before targetπ§Ό Clean and Safe Approach (Using Dummy Node)Using dummy node avoids edge cases like removing head.π» Code (Optimal One Pass Solution)class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { // Dummy node to handle edge cases ListNode dummy = new ListNode(0, head); ListNode fast = dummy; ListNode slow = dummy; // Move fast pointer n steps ahead for(int i = 0; i < n; i++){ fast = fast.next; } // Move both pointers while(fast.next != null){ fast = fast.next; slow = slow.next; } // Remove nth node slow.next = slow.next.next; return dummy.next; }}β±οΈ ComplexityTime ComplexityO(n)(single pass)Space ComplexityO(1)βοΈ Comparing ApproachesApproachPassesTimeSpaceDifficultyCounting2O(n)O(1)EasyTwo Pointer1O(n)O(1)Optimalβ Common MistakesForgetting to handle removing head nodeNot using dummy nodeOff-by-one errors in pointer movementMoving fast incorrectlyπ₯ Interview InsightThis problem is a classic example of:Fast & Slow Pointer TechniqueUsed in many problems like:Cycle DetectionMiddle of Linked ListPalindrome Linked Listπ§ Final ThoughtAt first, counting feels naturalβ¦But once you learn this trick:"Create a gap and move together"π You unlock a powerful pattern.π ConclusionThe Remove Nth Node From End problem is not just about deletionβ¦It teaches:Efficient traversalPointer coordinationOne-pass optimizationπ Tip: Whenever you see βfrom endβ, think:"Can I use two pointers with a gap?"Thatβs your shortcut to solving these problems like a pro π









