> For the complete documentation index, see [llms.txt](https://code-snippets.hbamithkumara.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://code-snippets.hbamithkumara.com/leetcode/problems/1701-1800/swapping-nodes-in-a-linked-list.md).

# 1721. Swapping Nodes in a Linked List

### Description

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**).*

### Constraints

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

### Approach

### Links

* GeeksforGeeks
* [Leetcode](https://leetcode.com/problems/swapping-nodes-in-a-linked-list/)
* ProgramCreek
* YouTube

### **Examples**

{% tabs %}
{% tab title="Example 1" %}
**Input:** head = \[1, 2, 3, 4, 5], k = 2

**Output:** \[1, 4, 3, 2, 5]
{% endtab %}

{% tab title="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]
{% endtab %}

{% tab title="Example 3" %}
**Input:** head = \[1], k = 1

**Output:** \[1]
{% endtab %}

{% tab title="Example 4" %}
**Input:** head = \[1, 2], k = 1

**Output:** \[2, 1]
{% endtab %}

{% tab title="Example 5" %}
**Input:** head = \[1, 2, 3], k = 2

**Output:** \[1, 2, 3]
{% endtab %}
{% endtabs %}

### **Solutions**

{% tabs %}
{% tab title="ListNode" %}

```java
// Definition for singly-linked list.
public class ListNode {
	int val;
	ListNode next;
	ListNode() {}
	ListNode(int val) {
		this.val = val;
	}
	ListNode(int val, ListNode next) { 
		this.val = val; this.next = next;
	}
}
```

{% endtab %}

{% tab title="Solution 1" %}

```java
/**
 * Time complexity : O(N)
 * Space complexity : O(1)
 */

class Solution {
    public ListNode swapNodes(ListNode head, int k) {
        if(head == null || (head.next == null && k == 1)) {
            return head;
        }
        
        ListNode kthNodeFromStart = head, kthNodeFromEnd = head, temp = head;
        
        while(k-- > 1) {
            kthNodeFromStart = kthNodeFromStart.next;
            temp = temp.next;
        }
        
        while(temp.next != null) {
            kthNodeFromEnd = kthNodeFromEnd.next;
            temp = temp.next;
        }
        
        int val = kthNodeFromStart.val;
        kthNodeFromStart.val = kthNodeFromEnd.val;
        kthNodeFromEnd.val = val;
        
        return head;
    }
}
```

{% endtab %}

{% tab title="Solution 2" %}

```java
/**
 * Time complexity : O(n), where n is the size of Linked List. 
 *    We are iterating over the entire Linked List once.
 * Space complexity : O(1), as we are using constant extra space to maintain 
 *    list node pointers frontNode, endNode and currentNode.
 */
 
 class Solution {
    public ListNode swapNodes(ListNode head, int k) {
        int listLength = 0;
        ListNode frontNode = null;
        ListNode endNode = null;
        ListNode currentNode = head;
        
        // set the front node and end node in single pass
        while (currentNode != null) {
            listLength++;
            if (endNode != null)
                endNode = endNode.next;
            // check if we have reached kth node
            if (listLength == k) {
                frontNode = currentNode;
                endNode = head;
            }
            currentNode = currentNode.next;
        }
        
        // swap the values of front node and end node
        int temp = frontNode.val;
        frontNode.val = endNode.val;
        endNode.val = temp;
        
        return head;
    }
}
```

{% endtab %}
{% endtabs %}

### **Follow up**

*
