{"id":9415,"date":"2022-01-09T03:40:22","date_gmt":"2022-01-09T11:40:22","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9415"},"modified":"2022-01-09T03:41:01","modified_gmt":"2022-01-09T11:41:01","slug":"leetcode-2130-maximum-twin-sum-of-a-linked-list","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-2130-maximum-twin-sum-of-a-linked-list\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2130. Maximum Twin Sum of a Linked List"},"content":{"rendered":"\n<p>In a linked list of size&nbsp;<code>n<\/code>, where&nbsp;<code>n<\/code>&nbsp;is&nbsp;<strong>even<\/strong>, the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;node (<strong>0-indexed<\/strong>) of the linked list is known as the&nbsp;<strong>twin<\/strong>&nbsp;of the&nbsp;<code>(n-1-i)<sup>th<\/sup><\/code>&nbsp;node, if&nbsp;<code>0 &lt;= i &lt;= (n \/ 2) - 1<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, if&nbsp;<code>n = 4<\/code>, then node&nbsp;<code>0<\/code>&nbsp;is the twin of node&nbsp;<code>3<\/code>, and node&nbsp;<code>1<\/code>&nbsp;is the twin of node&nbsp;<code>2<\/code>. These are the only nodes with twins for&nbsp;<code>n = 4<\/code>.<\/li><\/ul>\n\n\n\n<p>The&nbsp;<strong>twin sum&nbsp;<\/strong>is defined as the sum of a node and its twin.<\/p>\n\n\n\n<p>Given the&nbsp;<code>head<\/code>&nbsp;of a linked list with even length, return&nbsp;<em>the&nbsp;<strong>maximum twin sum<\/strong>&nbsp;of the linked list<\/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\/2021\/12\/03\/eg1drawio.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [5,4,2,1]\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong>\nNodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6.\nThere are no other nodes with twins in the linked list.\nThus, the maximum twin sum of the linked list is 6. \n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/12\/03\/eg2drawio.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [4,2,2,3]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong>\nThe nodes with twins present in this linked list are:\n- Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7.\n- Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4.\nThus, the maximum twin sum of the linked list is max(7, 4) = 7. \n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/12\/03\/eg3drawio.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [1,100000]\n<strong>Output:<\/strong> 100001\n<strong>Explanation:<\/strong>\nThere is only one node with a twin in the linked list having twin sum of 1 + 100000 = 100001.\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 an&nbsp;<strong>even<\/strong>&nbsp;integer in the range&nbsp;<code>[2, 10<sup>5<\/sup>]<\/code>.<\/li><li><code>1 &lt;= Node.val &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Two Pointers + Reverse List<\/strong><\/h2>\n\n\n\n<p>Use fast slow pointers to find the middle point and reverse the second half.<\/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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int pairSum(ListNode* head) {\n    auto reverse = [](ListNode* head, ListNode* prev = nullptr) {\n      while (head) {\n        swap(head->next, prev);\n        swap(head, prev);\n      }\n      return prev;\n    };\n    \n    ListNode* fast = head;\n    ListNode* slow = head;\n    while (fast) {\n      fast = fast->next->next;\n      slow = slow->next;\n    }\n    \n    slow = reverse(slow);\n    int ans = 0;\n    while (slow) {\n      ans = max(ans, head->val + slow->val);\n      head = head->next;\n      slow = slow->next;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In a linked list of size&nbsp;n, where&nbsp;n&nbsp;is&nbsp;even, the&nbsp;ith&nbsp;node (0-indexed) of the linked list is known as the&nbsp;twin&nbsp;of the&nbsp;(n-1-i)th&nbsp;node, if&nbsp;0 &lt;= i &lt;= (n \/ 2)&#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,246,175],"class_list":["post-9415","post","type-post","status-publish","format-standard","hentry","category-list","tag-list","tag-medium","tag-reverse","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9415","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=9415"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9415\/revisions"}],"predecessor-version":[{"id":9417,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9415\/revisions\/9417"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}