LeetCode 2130. Maximum Twin Sum of a Linked List

2026. 1. 23. 17:33·코딩테스트/LeetCode

2130. Maximum Twin Sum of a Linked List

 

Problem


Solution

The key requirements of this problem are as follows:

  1. The linked list has an even legnth.
  2. The ith node is paired with the (length-1-i)th node.
  3. Return the maximum sum among all twin pairs.

To satisfy these requirements, I used a two-pointer approach.

First, I split the problem into three steps.

  1. Find the middle nodes of the list.
  2. Reverse the second half of the list.
  3. Compare the two halves node by node and track the maximum sum.

To find the middle, I use slow and fast pointer.

  • slow starts as head and moves single step at a time.
  • fast starts as head and moves two steps at a time.

When fast reaches the end, slow points to the middle of the list.

To reverse the second half, initialize three pointers.

  • prev starts as None.
  • rev starts as slow.
  • ahead is used to store the next node during iteration.

During reverse is like below.

  1. Store rev.next in ahead.
  2. Change rev.next to point to prev.
  3. Move prev to rev
  4. Move rev to ahead.

Repeat until the second half is fully reversed.

 

After that, prev points to the head of the reversed half.

 

Now, simply compare the two halves.

 

In each iteration, add prev.val and head.val and update when result is smaller than the sum.

 

At the end of the loop, return the result.

 

 


class Solution(object):
    def pairSum(self, head):

        slow = fast = head
        result = 0

        while fast:
            fast = fast.next.next
            slow = slow.next

        prev = None
        rev = slow

        while rev:
            ahead = rev.next
            rev.next = prev
            prev = rev
            rev = ahead

        while prev:
            temp = prev.val+head.val
            if temp > result:
                result = temp
            prev = prev.next
            head = head.next

        return result 

 

https://github.com/K-MarkLee/LeetCode-Practice

 

GitHub - K-MarkLee/LeetCode-Practice: A collection of LeetCode questions to ace the coding interview! - Created using [LeetHub 2

A collection of LeetCode questions to ace the coding interview! - Created using [LeetHub 2.0](https://github.com/maitreya2954/LeetHub-2.0-Firefox) - K-MarkLee/LeetCode-Practice

github.com

 

저작자표시 비영리 변경금지 (새창열림)

'코딩테스트 > LeetCode' 카테고리의 다른 글

LeetCode 872. Leaf-Similar Trees  (0) 2026.01.24
LeetCode 104. Maximum Depth of Binary Tree  (0) 2026.01.24
LeetCode 206. Reverse Linked List  (0) 2026.01.22
LeetCode 328. Odd Even Linked List  (0) 2026.01.21
LeetCode 2095. Delete the Middle Node of a Linked List  (0) 2026.01.17
'코딩테스트/LeetCode' 카테고리의 다른 글
  • LeetCode 872. Leaf-Similar Trees
  • LeetCode 104. Maximum Depth of Binary Tree
  • LeetCode 206. Reverse Linked List
  • LeetCode 328. Odd Even Linked List
코드 유랑자 승열
코드 유랑자 승열
코드 유랑자 승열의 프로그래밍 일지를 남기는 공간입니다.
  • 코드 유랑자 승열
    승열의 프로그래밍 시네마
    코드 유랑자 승열
  • 전체
    오늘
    어제
  • 링크

    • 깃허브 보러가기
    • 링크드인 보러가기
    • 카테고리
      • 코딩테스트
        • LeetCode
        • BaekJoon
      • TIL and WIL
        • TIL
        • WIL
      • 주말스터디
      • 내일배움캠프
        • 사전캠프 강의 (SQL)
      • 용어정리
        • Python
        • Python-Library
        • Machine-Learning
        • Deep-Learning
        • AI 활용
        • LLM & RAG
        • Docker
        • Django
        • SQL
        • Java Script
        • etc
      • Daily 코드카타
        • SQL
        • Python 알고리즘
      • 임시저장
      • 보류
  • 태그

    template
    word2vec
    View
    오블완
    llm
    vector db
    langchain
    티스토리챌린지
    RAG
    django
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
코드 유랑자 승열
LeetCode 2130. Maximum Twin Sum of a Linked List
상단으로

티스토리툴바