[LeetCode] 2130. Maximum Twin Sum of a Linked List

In a linked list of size n, where n is even, the ith node (0-indexed) of the linked list is known as the twin of the (n-1-i)th node, if 0 <= i <= (n / 2) - 1.

  • For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4.

The twin sum is defined as the sum of a node and its twin.

Given the head of a linked list with even length, return the maximum twin sum of the linked list.

Example 1:

Input: head = [5,4,2,1]
Output: 6
Explanation:
Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6.
There are no other nodes with twins in the linked list.
Thus, the maximum twin sum of the linked list is 6. 

Example 2:

Input: head = [4,2,2,3]
Output: 7
Explanation:
The nodes with twins present in this linked list are:
- Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7.
- Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4.
Thus, the maximum twin sum of the linked list is max(7, 4) = 7. 

Example 3:

Input: head = [1,100000]
Output: 100001
Explanation:
There is only one node with a twin in the linked list having twin sum of 1 + 100000 = 100001.

Constraints:

  • The number of nodes in the list is an even integer in the range [2, 105].
  • 1 <= Node.val <= 105

链表最大孪生和。

在一个大小为 n 且 n 为 偶数 的链表中,对于 0 <= i <= (n / 2) - 1 的 i ,第 i 个节点(下标从 0 开始)的孪生节点为第 (n-1-i) 个节点 。

比方说,n = 4 那么节点 0 是节点 3 的孪生节点,节点 1 是节点 2 的孪生节点。这是长度为 n = 4 的链表中所有的孪生节点。
孪生和 定义为一个节点和它孪生节点两者值之和。

给你一个长度为偶数的链表的头节点 head ,请你返回链表的 最大孪生和 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximum-twin-sum-of-a-linked-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题思路跟234题一样,思路还是找到链表的中点,反转后半部分,然后前半后半同时遍历,以找到最大孪生和。

时间O(n)

空间O(1)

Java实现

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode() {}
 7  *     ListNode(int val) { this.val = val; }
 8  *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 9  * }
10  */
11 class Solution {
12     public int pairSum(ListNode head) {
13         // corner case
14         if (head == null) {
15             return 0;
16         }
17 
18         // normal case
19         ListNode slow = head;
20         ListNode fast = head;
21         while (fast.next != null && fast.next.next != null) {
22             slow = slow.next;
23             fast = fast.next.next;
24         }
25         ListNode middle = slow;
26         middle.next = reverse(middle.next);
27         ListNode p1 = head;
28         ListNode p2 = middle.next;
29         // 也许要把两部分链表分开,这里不分开也能AC
30         // middle.next = null;
31         int max = 0;
32         while (p1 != null && p2 != null) {
33             max = Math.max(max, p1.val + p2.val);
34             p1 = p1.next;
35             p2 = p2.next;
36         }
37         return max;
38     }
39 
40     private ListNode reverse(ListNode head) {
41         ListNode pre = null;
42         while (head != null) {
43             ListNode next = head.next;
44             head.next = pre;
45             pre = head;
46             head = next;
47         }
48         return pre;
49     }
50 }

 

相关题目

125. Valid Palindrome

234. Palindrome Linked List

680. Valid Palindrome II

2130. Maximum Twin Sum of a Linked List

LeetCode 题目总结

posted @ 2022-06-30 10:32  CNoodle  阅读(298)  评论(0编辑  收藏  举报