LeetCode 2130. Maximum Twin Sum of a Linked List

原题链接在这里:https://leetcode.com/problems/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

题解:

Find mid node, reverse the list after midNext, its new head is newHead.

Now we have head and newHead, they are twins.

Iterate through 2 half lists and update the maximum.

Time Complexity: O(n). n is the length of list. findMid takes O(n). reverse takes O(n).

Space: O(1).

AC 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         if(head == null){
14             return 0;
15         }
16         
17         ListNode mid = findMid(head);
18         ListNode midNext = mid.next;
19         mid.next = null;
20         midNext = reverse(midNext);
21         int res = Integer.MIN_VALUE;
22         while(head != null && midNext != null){
23             res = Math.max(res, head.val + midNext.val);
24             head = head.next;
25             midNext = midNext.next;
26         }
27         
28         return res;
29     }
30     
31     private ListNode findMid(ListNode head){
32         if(head == null || head.next == null){
33             return head;
34         }
35         
36         ListNode runner = head;
37         ListNode walker = head;
38         while(runner.next != null && runner.next.next != null){
39             walker = walker.next;
40             runner = runner.next.next;
41         }
42         
43         return walker;
44     }
45     
46     private ListNode reverse(ListNode head){
47         if(head == null || head.next == null){
48             return head;
49         }
50         
51         ListNode tail = head;
52         ListNode cur = head;
53         ListNode pre;
54         ListNode temp;
55         while(tail.next != null){
56             pre = cur;
57             cur = tail.next;
58             temp = cur.next;
59             cur.next = pre;
60             tail.next = temp;
61         }
62         
63         return cur;
64     }
65 }
复制代码

类似Palindrome Linked List.

posted @   Dylan_Java_NYC  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2017-06-15 LeetCode 557. Reverse Words in a String III
点击右上角即可分享
微信分享提示