-
Notifications
You must be signed in to change notification settings - Fork 2
/
TwinSum.java
51 lines (44 loc) · 1.09 KB
/
TwinSum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com;
import java.util.ArrayList;
import java.util.List;
/**
* @author KOTBI Abderrahmane
* @version 1.1
*/
public class TwinSum {
/** This class has been added from leetcode to define the singly linked list. */
public static class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
/**
* @param head
* @return the maximum sum of pair numbers in the linked list head. You can find detailed
* explanaition of what are pair numbers in the related leetcode problem.
*/
public static int pairSum(ListNode head) {
ListNode acctual = head;
List<Integer> values = new ArrayList<Integer>();
while (acctual != null) {
values.add(acctual.val);
acctual = acctual.next;
}
int sum = 0;
int best = 0;
for (int i = 0; i < values.size() / 2; i++) {
sum = values.get(i) + values.get(values.size() - 1 - i);
if (sum > best) {
best = sum;
}
}
return best;
}
}