middle-of-linked-list.dart (574B)
1 //Write a program to return a linked list starting from the mid point 2 //of the inputted linked list. If the midpoint is between two values return the one on the right. 3 //This algorithm is O(n) time complexity where n is the length of the list. 4 //Time: 252ms Beats: 73.27% 5 //Memory: 143.1MB Beats: 21.78% 6 7 class Solution { 8 ListNode? middleNode(ListNode? head) { 9 List<ListNode> nodes = []; 10 while(head != null){ 11 nodes.add(head); 12 head = head.next; 13 } 14 ListNode mid_node = nodes[ (nodes.length / 2).toInt() ]; 15 return mid_node; 16 } 17 }