Linked List Node Swap with Two-Pointer Technique

Linked Lists always test one thing seriously — pointer control. Today’s problem reminded me of that again. 🚀 Day 11/100 – #100DaysOfDSA 🧩 Problem: Swapping Nodes in a Linked List (LeetCode 1721) 🧠 Problem Understanding Given a linked list and an integer k, we need to swap the value of the k-th node from the beginning and the k-th node from the end. Example: 1 → 2 → 3 → 4 → 5 k = 2 After swapping: 1 → 4 → 3 → 2 → 5 ⚡ Key Idea Instead of calculating the length and traversing twice, this can be solved efficiently using the two-pointer technique. Steps I used: • Traverse to reach the k-th node from the start • Start another pointer from the head • Move both pointers together until the end • The second pointer reaches the k-th node from the end • Finally, swap their values 💡 Concepts Practiced • Linked List traversal • Two-pointer technique • Pointer control and node tracking 📈 Reflection Problems like these remind me that DSA is less about writing long code and more about thinking clearly about the structure. Small problems. Stronger fundamentals. Day 11 done. On to Day 12. 🚀 ❓ Question for fellow developers: Have you solved this problem using a different approach? #100DaysOfCode #DSA #LeetCode #Cpp #LinkedList #ProblemSolving #CodingJourney

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories