commit e60c452bb8114aad85aa2b54fee7d61a6510545a parent 49f87a233d43b3d2b846d179ca54d5cf8c5c93c3 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Tue, 16 May 2023 20:41:24 -0500 Completed maximum twin sum of a linked list using dart Diffstat:
| A | maximum-twin-sum-of-a-linked-list/maximum-twin-sum-of-a-linked-list.dart | | | 23 | +++++++++++++++++++++++ |
1 file changed, 23 insertions(+), 0 deletions(-)
diff --git a/maximum-twin-sum-of-a-linked-list/maximum-twin-sum-of-a-linked-list.dart b/maximum-twin-sum-of-a-linked-list/maximum-twin-sum-of-a-linked-list.dart @@ -0,0 +1,23 @@ +//Find the maximum value of all twins in a linked list. +//A twin is the n and linked-list.length - 1 - n values combined together. +//The time complexity of my code is O(n) because it simply iterates through the list +//once and then again a second time. +//Time: 424ms Beats: 100% +//Memory: 211.5MB Beats: 50% + +class Solution { + int pairSum(ListNode? head) { + List<int> vals = []; + while(head != null){ + vals.add(head?.val ?? 0); + head = head?.next; + } + int max = 0; + for(int i = 0 ; i < vals.length / 2 ; ++i){ + if(max < vals[i] + vals[vals.length - 1 - i]){ + max = vals[i] + vals[vals.length - 1 - i]; + } + } + return max; + } +}