Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/1721.html
1721. Swapping Nodes in a Linked List
Level
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; return head; } } ############ /** * 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; return head; } }
-
// 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; for (; head; head = head->next) ++ans; return ans; } ListNode *getNode(ListNode *head, int k) { while (--k) head = head->next; return head; } 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); return head; } };
-
# 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: fast = head for _ in range(k - 1): fast = fast.next p = fast slow = head while fast.next: slow, fast = slow.next, fast.next q = slow p.val, q.val = q.val, p.val return head ############ # 1721. Swapping Nodes in a Linked List # https://leetcode.com/problems/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 = [] curr = head while curr: res.append(curr) curr = curr.next res[k-1].val, res[-k].val = res[-k].val, res[k-1].val return head
-
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ func swapNodes(head *ListNode, k int) *ListNode { fast := head for k > 1 { fast = fast.Next k-- } p := fast slow := head for fast.Next != nil { slow, fast = slow.Next, fast.Next } q := slow p.Val, q.Val = q.Val, p.Val return head }
-
/** * 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]; return head; }
-
/** * 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; return head; } }