02. Swap Kth Nodes from Ends
β GFG solution to swap kth nodes from beginning and end of a singly linked list: efficient pointer manipulation technique with optimal time complexity. π
π§© Problem Description
π Examples
Example 1
Input: k = 1, List: 1 -> 2 -> 3 -> 4 -> 5
Output: 5 -> 2 -> 3 -> 4 -> 1
Explanation: Here k = 1, hence after swapping the 1st node from the beginning
and end the new list will be 5 -> 2 -> 3 -> 4 -> 1.Example 2
Input: k = 2, List: 5 -> 7 -> 8 -> 5 -> 10 -> 3
Output: 5 -> 9 -> 8 -> 5 -> 10 -> 3
Explanation: Here k = 2, hence after swapping the 2nd node from the beginning
and end the new list will be 5 -> 9 -> 8 -> 5 -> 10 -> 3.Example 3
π Constraints
β
My Approach
Optimized Single Pass Algorithm
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated