##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1721.html

# 1721. Swapping Nodes in a Linked List

Medium

## 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 k-th node from the beginning and the k-th 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]

Example 3:

Input: head = [1], k = 1

Output: [1]

Example 4:

Input: head = [1,2], k = 1

Output: [2,1]

Example 5:

Input: head = [1,2,3], k = 2

Output: [1,2,3]

Constraints:

• The number of nodes in the list is n.
• 1 <= k <= n <= 10^5
• 0 <= Node.val <= 100

## Solution

Use pointers to obtain the two nodes, which are node1 and node2. The node node1 is the k-th node starting from the beginning, and the node node2 is the k-th node from the end. Then obtain the two nodes’ values and swap their values.

• /**
* 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; }
* }
*/
class Solution {
public ListNode swapNodes(ListNode head, int k) {
ListNode node1 = head;
for (int i = 1; i < k; i++)
node1 = node1.next;
ListNode node2 = head, temp = node1;
while (temp.next != null) {
node2 = node2.next;
temp = temp.next;
}
int val1 = node1.val, val2 = node2.val;
node1.val = val2;
node2.val = val1;
}
}

############

/**
* 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; }
* }
*/
class Solution {
public ListNode swapNodes(ListNode head, int k) {
ListNode fast = head;
while (--k > 0) {
fast = fast.next;
}
ListNode p = fast;
ListNode slow = head;
while (fast.next != null) {
slow = slow.next;
fast = fast.next;
}
ListNode q = slow;
int t = p.val;
p.val = q.val;
q.val = t;
}
}

• // OJ: https://leetcode.com/problems/swapping-nodes-in-a-linked-list/
// Time: O(N)
// Space: O(1)
class Solution {
int getLength(ListNode *head) {
int ans = 0;
return ans;
}
ListNode *getNode(ListNode *head, int k) {
}
public:
ListNode* swapNodes(ListNode* head, int k) {
int len = getLength(head), r = len - k + 1;
if (r == k) return head;
ListNode *a = getNode(head, k), *b = getNode(head, r);
swap(a->val, b->val);
}
};

• # Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
def swapNodes(self, head: ListNode, k: int) -> ListNode:
for _ in range(k - 1):
fast = fast.next
p = fast
while fast.next:
slow, fast = slow.next, fast.next
q = slow
p.val, q.val = q.val, p.val

############

# 1721. Swapping Nodes in a Linked List

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
def swapNodes(self, head: ListNode, k: int):
res = []

while curr:
res.append(curr)
curr = curr.next

res[k-1].val, res[-k].val = res[-k].val, res[k-1].val


• /**
* Definition for singly-linked list.
* type ListNode struct {
*     Val int
*     Next *ListNode
* }
*/
func swapNodes(head *ListNode, k int) *ListNode {
for k > 1 {
fast = fast.Next
k--
}
p := fast
for fast.Next != nil {
slow, fast = slow.Next, fast.Next
}
q := slow
p.Val, q.Val = q.Val, p.Val
}

• /**
* Definition for singly-linked list.
* class ListNode {
*     val: number
*     next: ListNode | null
*     constructor(val?: number, next?: ListNode | null) {
*         this.val = (val===undefined ? 0 : val)
*         this.next = (next===undefined ? null : next)
*     }
* }
*/

function swapNodes(head: ListNode | null, k: number): ListNode | null {
let fast = head;
while (--k) {
fast = fast.next;
}
let p = fast;
let slow = head;
while (fast.next) {
slow = slow.next;
fast = fast.next;
}
let q = slow;
[p.val, q.val] = [q.val, p.val];
}


• /**
* Definition for singly-linked list.
* public class ListNode {
*     public int val;
*     public ListNode next;
*     public ListNode(int val=0, ListNode next=null) {
*         this.val = val;
*         this.next = next;
*     }
* }
*/
public class Solution {
public ListNode SwapNodes(ListNode head, int k) {
ListNode fast = head;
while (--k > 0) {
fast = fast.next;
}
ListNode p = fast;
ListNode slow = head;
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
ListNode q = slow;
int t = p.val;
p.val = q.val;
q.val = t;