commit dc9e0141c5c62c928c5a2b8fa4e6e37e869ad3fe parent 897a722df2a57e8aaac99f88a8eaf41c712022e8 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Sun, 30 Apr 2023 20:13:09 -0500 Completed middle of linked list problem using dart Diffstat:
| A | middle-of-linked-list/middle-of-linked-list.dart | | | 17 | +++++++++++++++++ |
1 file changed, 17 insertions(+), 0 deletions(-)
diff --git a/middle-of-linked-list/middle-of-linked-list.dart b/middle-of-linked-list/middle-of-linked-list.dart @@ -0,0 +1,17 @@ +//Write a program to return a linked list starting from the mid point +//of the inputted linked list. If the midpoint is between two values return the one on the right. +//This algorithm is O(n) time complexity where n is the length of the list. +//Time: 252ms Beats: 73.27% +//Memory: 143.1MB Beats: 21.78% + +class Solution { + ListNode? middleNode(ListNode? head) { + List<ListNode> nodes = []; + while(head != null){ + nodes.add(head); + head = head.next; + } + ListNode mid_node = nodes[ (nodes.length / 2).toInt() ]; + return mid_node; + } +}