Skip to content

Commit

Permalink
Create: 0021-Merge-Two-Sorted-Lists.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
laitooo committed Jan 8, 2023
1 parent ff32745 commit 3ff6b7a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions dart/0021-merge-two-sorted-lists.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode? next;
* ListNode([this.val = 0, this.next]);
* }
*/
class Solution {
ListNode? mergeTwoLists(ListNode? list1, ListNode? list2) {
ListNode? head = ListNode();
ListNode? cur = head;
while (list1 != null && list2 != null) {
if (list1!.val < list2!.val) {
cur!.next = list1;
list1 = list1!.next;
} else {
cur!.next = list2;
list2 = list2!.next;
}
cur = cur!.next;
}
cur!.next = (list1 == null) ? list2 : list1;
return head.next;
}
}

0 comments on commit 3ff6b7a

Please sign in to comment.