Skip to content

Commit 59ea62e

Browse files
committed
feat: add solutions to lc problem: No.2130
No.2130.Maximum Twin Sum of a Linked List
1 parent bd4f219 commit 59ea62e

File tree

6 files changed

+466
-24
lines changed

6 files changed

+466
-24
lines changed

solution/2100-2199/2130.Maximum Twin Sum of a Linked List/README.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@
6666

6767
<!-- 这里可写通用的实现逻辑 -->
6868

69+
**方法一:链表转成列表(数组)求解**
70+
71+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
72+
73+
**方法二:快慢指针 + 反转链表 + 双指针**
74+
75+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
76+
6977
<!-- tabs:start -->
7078

7179
### **Python3**
@@ -88,6 +96,39 @@ class Solution:
8896
return max(s[i] + s[-(i + 1)] for i in range(n >> 1))
8997
```
9098

99+
```python
100+
# Definition for singly-linked list.
101+
# class ListNode:
102+
# def __init__(self, val=0, next=None):
103+
# self.val = val
104+
# self.next = next
105+
class Solution:
106+
def pairSum(self, head: Optional[ListNode]) -> int:
107+
def reverse(head):
108+
dummy = ListNode()
109+
curr = head
110+
while curr:
111+
next = curr.next
112+
curr.next = dummy.next
113+
dummy.next = curr
114+
curr = next
115+
return dummy.next
116+
117+
slow, fast = head, head.next
118+
while fast and fast.next:
119+
slow, fast = slow.next, fast.next.next
120+
pa = head
121+
q = slow.next
122+
slow.next = None
123+
pb = reverse(q)
124+
ans = 0
125+
while pa and pb:
126+
ans = max(ans, pa.val + pb.val)
127+
pa = pa.next
128+
pb = pb.next
129+
return ans
130+
```
131+
91132
### **Java**
92133

93134
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -118,6 +159,52 @@ class Solution {
118159
}
119160
```
120161

162+
```java
163+
/**
164+
* Definition for singly-linked list.
165+
* public class ListNode {
166+
* int val;
167+
* ListNode next;
168+
* ListNode() {}
169+
* ListNode(int val) { this.val = val; }
170+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
171+
* }
172+
*/
173+
class Solution {
174+
public int pairSum(ListNode head) {
175+
ListNode slow = head;
176+
ListNode fast = head.next;
177+
while (fast != null && fast.next != null) {
178+
slow = slow.next;
179+
fast = fast.next.next;
180+
}
181+
ListNode pa = head;
182+
ListNode q = slow.next;
183+
slow.next = null;
184+
ListNode pb = reverse(q);
185+
int ans = 0;
186+
while (pa != null) {
187+
ans = Math.max(ans, pa.val + pb.val);
188+
pa = pa.next;
189+
pb = pb.next;
190+
}
191+
return ans;
192+
}
193+
194+
private ListNode reverse(ListNode head) {
195+
ListNode dummy = new ListNode();
196+
ListNode curr = head;
197+
while (curr != null) {
198+
ListNode next = curr.next;
199+
curr.next = dummy.next;
200+
dummy.next = curr;
201+
curr = next;
202+
}
203+
return dummy.next;
204+
}
205+
}
206+
```
207+
121208
### **C++**
122209

123210
```cpp
@@ -143,6 +230,56 @@ public:
143230
};
144231
```
145232
233+
```cpp
234+
/**
235+
* Definition for singly-linked list.
236+
* struct ListNode {
237+
* int val;
238+
* ListNode *next;
239+
* ListNode() : val(0), next(nullptr) {}
240+
* ListNode(int x) : val(x), next(nullptr) {}
241+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
242+
* };
243+
*/
244+
class Solution {
245+
public:
246+
int pairSum(ListNode* head) {
247+
ListNode* slow = head;
248+
ListNode* fast = head->next;
249+
while (fast && fast->next)
250+
{
251+
slow = slow->next;
252+
fast = fast->next->next;
253+
}
254+
ListNode* pa = head;
255+
ListNode* q = slow->next;
256+
slow->next = nullptr;
257+
ListNode* pb = reverse(q);
258+
int ans = 0;
259+
while (pa)
260+
{
261+
ans = max(ans, pa->val + pb->val);
262+
pa = pa->next;
263+
pb = pb->next;
264+
}
265+
return ans;
266+
}
267+
268+
ListNode* reverse(ListNode* head) {
269+
ListNode* dummy = new ListNode();
270+
ListNode* curr = head;
271+
while (curr)
272+
{
273+
ListNode* next = curr->next;
274+
curr->next = dummy->next;
275+
dummy->next = curr;
276+
curr = next;
277+
}
278+
return dummy->next;
279+
}
280+
};
281+
```
282+
146283
### **Go**
147284

148285
```go
@@ -168,6 +305,51 @@ func pairSum(head *ListNode) int {
168305
}
169306
```
170307

308+
```go
309+
/**
310+
* Definition for singly-linked list.
311+
* type ListNode struct {
312+
* Val int
313+
* Next *ListNode
314+
* }
315+
*/
316+
func pairSum(head *ListNode) int {
317+
reverse := func(head *ListNode) *ListNode {
318+
dummy := &ListNode{}
319+
curr := head
320+
for curr != nil {
321+
next := curr.Next
322+
curr.Next = dummy.Next
323+
dummy.Next = curr
324+
curr = next
325+
}
326+
return dummy.Next
327+
}
328+
slow, fast := head, head.Next
329+
for fast != nil && fast.Next != nil {
330+
slow, fast = slow.Next, fast.Next.Next
331+
}
332+
pa := head
333+
q := slow.Next
334+
slow.Next = nil
335+
pb := reverse(q)
336+
ans := 0
337+
for pa != nil {
338+
ans = max(ans, pa.Val+pb.Val)
339+
pa = pa.Next
340+
pb = pb.Next
341+
}
342+
return ans
343+
}
344+
345+
func max(a, b int) int {
346+
if a > b {
347+
return a
348+
}
349+
return b
350+
}
351+
```
352+
171353
### **TypeScript**
172354

173355
<!-- 这里可写当前语言的特殊实现逻辑 -->

0 commit comments

Comments
 (0)