commit c4675f449b064c100b1ea5ecc93496e7a79e1008 parent c688629e2bcf1b51d04a0db85b1025c34aded5dc Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Tue, 18 Apr 2023 17:08:48 -0500 Merge singly linked lists with dart Diffstat:
| A | merge-two-sorted-lists/merge-two-sorted-lists.dart | | | 64 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 64 insertions(+), 0 deletions(-)
diff --git a/merge-two-sorted-lists/merge-two-sorted-lists.dart b/merge-two-sorted-lists/merge-two-sorted-lists.dart @@ -0,0 +1,64 @@ +//This was kind of annoying with the null safety of dart +//causing there to be ? everywhere. Also, dart is not made for pointers +//or singly linked lists. +//Time: 280ms Beats: 73.33% +//Memory 144.6MB Beats: 22.22% + +class ListNode{ + int val = 0; + ListNode? next; +} + +void main(){ + Solution sol = new Solution(); + ListNode head1 = new ListNode(); + ListNode ln1 = new ListNode(); + head1.val = 4; + ln1.val = 5; + head1.next = ln1; + ListNode ln2 = new ListNode(); + ln2.val = 10; + ln1.next = ln2; + ListNode? ln = sol.mergeTwoLists(head1, ln1); + while(ln?.val != null){ + print(ln?.val); + ln = ln?.next; + } +} + +class Solution { + ListNode? mergeTwoLists(ListNode? list1, ListNode? list2) { + if(list1?.val == null && list2?.val == null){ + return list1 ; + } + List<int?> vals = []; + while(list1?.val != null && list2?.val != null){ + vals.add(list2?.val); + vals.add(list1?.val); + list1 = list1?.next; + list2 = list2?.next; + } + + while(list1?.val != null){ + vals.add(list1?.val); + list1 = list1?.next; + } + while(list2?.val != null){ + vals.add(list2?.val); + list2 = list2?.next; + } + vals.sort(); + + ListNode ln = new ListNode(); + ListNode ln1 = new ListNode(); + ListNode start = ln; + for(int i = 0 ; i < vals.length - 1; ++i){ + ln.val = vals[i] ?? 0; + ln.next = ln1; + ln = ln1; + ln1 = new ListNode(); + } + ln.val = vals[vals.length - 1] ?? 0; + return start; + } +}