国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

LeetCode //C - 2130. Maximum Twin Sum of a Linked List

這篇具有很好參考價(jià)值的文章主要介紹了LeetCode //C - 2130. Maximum Twin Sum of a Linked List。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

2130. Maximum Twin Sum of a Linked List

In a linked list of size n, where n is even, the i t h i^{th} ith node (0-indexed) of the linked list is known as the twin of the ( n ? 1 ? i ) t h (n-1-i)^{th} (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:

LeetCode //C - 2130. Maximum Twin Sum of a Linked List,LeetCode,leetcode,c語(yǔ)言,算法

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:

LeetCode //C - 2130. Maximum Twin Sum of a Linked List,LeetCode,leetcode,c語(yǔ)言,算法

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:

LeetCode //C - 2130. Maximum Twin Sum of a Linked List,LeetCode,leetcode,c語(yǔ)言,算法

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 , 1 0 5 ] [2, 10^5] [2,105].
  • 1 < = N o d e . v a l < = 1 0 5 1 <= Node.val <= 10^5 1<=Node.val<=105

From: LeetCode
Link: 2130. Maximum Twin Sum of a Linked List文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-799319.html


Solution:

Ideas:
  1. Use the two-pointer technique to find the middle of the linked list.
  2. Reverse the second half of the list.
  3. Sum the values of the corresponding nodes from the start and reversed second half to find the maximum sum.
  4. Return the maximum sum found.
Code:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

int pairSum(struct ListNode* head) {
    if (head == NULL || head->next == NULL) return 0;
    
    // Step 1: Find the middle of the linked list
    struct ListNode *slow = head, *fast = head;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }
    
    // Step 2: Reverse the second half of the linked list
    struct ListNode *prev = NULL, *next = NULL;
    while (slow) {
        next = slow->next;
        slow->next = prev;
        prev = slow;
        slow = next;
    }
    
    // Step 3: Pairwise sum the values from the start and end to find the maximum sum
    int maxSum = 0;
    struct ListNode *start = head, *end = prev;
    while (end) {
        maxSum = maxSum > (start->val + end->val) ? maxSum : (start->val + end->val);
        start = start->next;
        end = end->next;
    }
    
    // Step 4: Return the maximum sum
    return maxSum;
}

到了這里,關(guān)于LeetCode //C - 2130. Maximum Twin Sum of a Linked List的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • LeetCode 918. Maximum Sum Circular Subarray【數(shù)組,動(dòng)態(tài)規(guī)劃】中等

    LeetCode 918. Maximum Sum Circular Subarray【數(shù)組,動(dòng)態(tài)規(guī)劃】中等

    本文屬于「征服LeetCode」系列文章之一,這一系列正式開(kāi)始于2021/08/12。由于LeetCode上部分題目有鎖,本系列將至少持續(xù)到刷完所有無(wú)鎖題之日為止;由于LeetCode還在不斷地創(chuàng)建新題,本系列的終止日期可能是永遠(yuǎn)。在這一系列刷題文章中,我不僅會(huì)講解多種解題思路及其優(yōu)化,

    2024年02月15日
    瀏覽(18)
  • LeetCode //C - 124. Binary Tree Maximum Path Sum

    LeetCode //C - 124. Binary Tree Maximum Path Sum

    A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root. The path sum of a path is the sum of the node’s values in the path. Given the root of a binary tree, return the maximum

    2024年02月09日
    瀏覽(25)
  • 【算法】Maximum Sum of Two Non-Overlapping Subarrays 兩個(gè)非重疊子數(shù)組的最大和

    問(wèn)題 有一個(gè)整數(shù)數(shù)組nums,和2個(gè)整數(shù)firstlen,secondlen,要求找出2個(gè)非重疊子數(shù)組中的元素的最大和,長(zhǎng)度分別是firstlen,secondlen。 不限制2個(gè)子數(shù)組的先后順序。 firstlen,secondlen的范圍 [ 1 , 1000 ] [1,1000] [ 1 , 1000 ] firstlen+secondlen的范圍 [ 2 , 1000 ] [2,1000] [ 2 , 1000 ] f i r s t l e n ,

    2023年04月27日
    瀏覽(354)
  • LeetCode142. Linked List Cycle II

    Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to (0-indexed). It

    2024年02月04日
    瀏覽(23)
  • LeetCode //C - 206. Reverse Linked List

    LeetCode //C - 206. Reverse Linked List

    Given the head of a singly linked list, reverse the list, and return the reversed list. ? Example 1: Input head = [1,2,3,4,5] Output [5,4,3,2,1] Example 2: Input head = [1,2] Output [2,1] Example 3: Input head = [] Output [] Constraints: The number of nodes in the list is the range [0, 5000]. -5000 = Node.val = 5000 From: LeetCode Link: 206. Reverse Linked

    2024年02月01日
    瀏覽(22)
  • LeetCode //C - 141. Linked List Cycle

    LeetCode //C - 141. Linked List Cycle

    Given head , the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a p

    2024年02月11日
    瀏覽(19)
  • LeetCode //C - 92. Reverse Linked List II

    LeetCode //C - 92. Reverse Linked List II

    Given the head of a singly linked list and two integers left and right where left = right, reverse the nodes of the list from position left to position right , and return the reversed list. ? Example 1: Input: head = [1,2,3,4,5], left = 2, right = 4 Output: [1,4,3,2,5] Example 2: Input: head = [5], left = 1, right = 1 Output: [5] Constraints: The number of

    2024年02月11日
    瀏覽(24)
  • 【set】個(gè)人練習(xí)-Leetcode-817. Linked List Components

    題目鏈接:https://leetcode.cn/problems/linked-list-components/description/ 題目大意:給出一個(gè) vectorint nums ,其中有一些數(shù)字。再給出一個(gè)鏈表的頭指針 head ,鏈表內(nèi)的元素各不相同。如果鏈表中有某一段(長(zhǎng)度大于等于1)的元素都在 nums 中出現(xiàn)過(guò),那么就算一個(gè)component,求鏈表中的co

    2024年02月13日
    瀏覽(22)
  • LeetCode //C - 328. Odd Even Linked List

    LeetCode //C - 328. Odd Even Linked List

    Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. The first node is considered odd, and the second node is even, and so on. Note that the relative order inside both the even and odd groups should remain as it was in the input. You must solve the proble

    2024年01月25日
    瀏覽(25)
  • LeetCode 92. Reverse Linked List II【鏈表,頭插法】中等

    LeetCode 92. Reverse Linked List II【鏈表,頭插法】中等

    本文屬于「征服LeetCode」系列文章之一,這一系列正式開(kāi)始于2021/08/12。由于LeetCode上部分題目有鎖,本系列將至少持續(xù)到刷完所有無(wú)鎖題之日為止;由于LeetCode還在不斷地創(chuàng)建新題,本系列的終止日期可能是永遠(yuǎn)。在這一系列刷題文章中,我不僅會(huì)講解多種解題思路及其優(yōu)化,

    2024年02月09日
    瀏覽(19)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包