-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2130.MaximumTwinSumofaLinkedList.kt
52 lines (48 loc) · 1.45 KB
/
2130.MaximumTwinSumofaLinkedList.kt
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
52
/**
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
//https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/description
class Solution {
fun pairSum(head: ListNode?): Int {
if(head == null) return 0
//1. find middle of linked list
val mid = getMiddle(head)
var left = head
var right = reverseLinkedList(mid?.next)
var maxTwinSum = Int.MIN_VALUE
// Step 2: Compare the first half (left) with the reversed second half(right)
var secondHalfPointer = right
while (secondHalfPointer != null){
maxTwinSum = maxOf(maxTwinSum, (left!!.`val` + secondHalfPointer.`val`))
left = left.next
secondHalfPointer = secondHalfPointer.next
}
return maxTwinSum
}
fun getMiddle(head: ListNode?): ListNode?{
var slow = head
var fast = head
while(fast?.next != null && fast.next?.next != null){
slow = slow?.next
fast = fast.next?.next
}
return slow
}
fun reverseLinkedList(head: ListNode?): ListNode?{
var prev: ListNode? = null
var current = head
while(current != null){
val temp = current.next
current.next = prev
prev = current
current = temp
}
return prev
}
}