{"id":7964,"date":"2021-01-10T09:59:01","date_gmt":"2021-01-10T17:59:01","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7964"},"modified":"2021-01-10T09:59:37","modified_gmt":"2021-01-10T17:59:37","slug":"leetcode-1721-swapping-nodes-in-a-linked-list","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-1721-swapping-nodes-in-a-linked-list\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1721. Swapping Nodes in a Linked List"},"content":{"rendered":"\n<p>You are given the&nbsp;<code>head<\/code>&nbsp;of a linked list, and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the head of the linked list after&nbsp;<strong>swapping<\/strong>&nbsp;the values of the&nbsp;<\/em><code>k<sup>th<\/sup><\/code>&nbsp;<em>node from the beginning and the&nbsp;<\/em><code>k<sup>th<\/sup><\/code>&nbsp;<em>node from the end (the list is&nbsp;<strong>1-indexed<\/strong>).<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/21\/linked1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [1,2,3,4,5], k = 2\n<strong>Output:<\/strong> [1,4,3,2,5]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [7,9,6,6,7,8,3,0,9,5], k = 5\n<strong>Output:<\/strong> [7,9,6,6,8,7,3,0,9,5]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [1], k = 1\n<strong>Output:<\/strong> [1]\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [1,2], k = 1\n<strong>Output:<\/strong> [2,1]\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [1,2,3], k = 2\n<strong>Output:<\/strong> [1,2,3]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes in the list is&nbsp;<code>n<\/code>.<\/li><li><code>1 &lt;= k &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= Node.val &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution:<\/strong><\/h2>\n\n\n\n<p>Two passes. First pass, find the length of the list. Second pass, record the k-th and n-k+1-th node.<br>Once done swap their values.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  ListNode* swapNodes(ListNode* head, int k) {\n    int l = 0;\n    ListNode* cur = head;    \n    while (cur) { cur = cur->next; ++l; }    \n    \n    cur = head;\n    ListNode* n1 = nullptr;\n    ListNode* n2 = nullptr;\n    for (int i = 1; i <= l; ++i, cur = cur->next) {\n      if (i == k) n1 = cur;\n      if (i == l - k + 1) n2 = cur;\n    }\n    swap(n1->val, n2->val);\n    return head;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given the&nbsp;head&nbsp;of a linked list, and an integer&nbsp;k. Return&nbsp;the head of the linked list after&nbsp;swapping&nbsp;the values of the&nbsp;kth&nbsp;node from the beginning and the&nbsp;kth&nbsp;node&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[83,177,290],"class_list":["post-7964","post","type-post","status-publish","format-standard","hentry","category-list","tag-list","tag-medium","tag-swap","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7964"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7964\/revisions"}],"predecessor-version":[{"id":7966,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7964\/revisions\/7966"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}