Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1962 from laitooo/0021-merge-two-sorte…
Browse files Browse the repository at this point in the history
…d-lists.dart

Create: 0021-Merge-Two-Sorted-Lists.dart
  • Loading branch information
tahsintunan authored Jan 9, 2023
2 parents 38d9425 + 3ff6b7a commit 9c6f4b2
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 9c6f4b2

Please sign in to comment.