1721. Swapping Nodes in a Linked List LeetCode Solution

In this guide, you will get 1721. Swapping Nodes in a Linked List LeetCode Solution with the best time and space complexity. The solution to Swapping Nodes in a Linked List problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Swapping Nodes in a Linked List solution in C++
  4. Swapping Nodes in a Linked List solution in Java
  5. Swapping Nodes in a Linked List solution in Python
  6. Additional Resources
1721. Swapping Nodes in a Linked List LeetCode Solution image

Problem Statement of Swapping Nodes in a Linked List

You are given the head of a linked list, and an integer k.
Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed).

Example 1:

Input: head = [1,2,3,4,5], k = 2
Output: [1,4,3,2,5]

Example 2:

Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5
Output: [7,9,6,6,8,7,3,0,9,5]

Constraints:

The number of nodes in the list is n.
1 <= k <= n <= 105
0 <= Node.val <= 100

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1721. Swapping Nodes in a Linked List LeetCode Solution in C++

class Solution {
 public:
  ListNode* swapNodes(ListNode* head, int k) {
    ListNode* p = nullptr;  // Points the k-th node from the beginning.
    ListNode* q = nullptr;  // Points the k-th node from the end.

    for (ListNode* curr = head; curr != nullptr; curr = curr->next) {
      if (q != nullptr)
        q = q->next;
      if (--k == 0) {
        p = curr;
        q = head;
      }
    }

    swap(p->val, q->val);
    return head;
  }
};
/* code provided by PROGIEZ */

1721. Swapping Nodes in a Linked List LeetCode Solution in Java

class Solution {
  public ListNode swapNodes(ListNode head, int k) {
    ListNode p = null; // Points the k-th node from the beginning.
    ListNode q = null; // Points the k-th node from the end.

    for (ListNode curr = head; curr != null; curr = curr.next) {
      if (q != null)
        q = q.next;
      if (--k == 0) {
        p = curr;
        q = head;
      }
    }

    final int temp = p.val;
    p.val = q.val;
    q.val = temp;
    return head;
  }
}
// code provided by PROGIEZ

1721. Swapping Nodes in a Linked List LeetCode Solution in Python

class Solution:
  def swapNodes(self, head: ListNode | None, k: int) -> ListNode | None:
    p = None  # Points the k-th node from the beginning.
    q = None  # Points the k-th node from the end.

    curr = head
    while curr:
      if q:
        q = q.next
      k -= 1
      if k == 0:
        p = curr
        q = head
      curr = curr.next

    p.val, q.val = q.val, p.val
    return head
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.