diff --git a/scala/21-Merge-Two-Sorted-Lists.scala b/scala/21-Merge-Two-Sorted-Lists.scala new file mode 100644 index 000000000..8354a2834 --- /dev/null +++ b/scala/21-Merge-Two-Sorted-Lists.scala @@ -0,0 +1,34 @@ +/** + * Definition for singly-linked list. + * class ListNode(_x: Int = 0, _next: ListNode = null) { + * var next: ListNode = _next + * var x: Int = _x + * } + */ +object Solution { + def mergeTwoLists(list1: ListNode, list2: ListNode): ListNode = { + val head = new ListNode() + var tail = head + var (ptr1, ptr2) = (list1, list2) + + while (ptr1 != null && ptr2 != null) { + if (ptr1.x <= ptr2.x) { + tail.next = ptr1 + ptr1 = ptr1.next + } else { + tail.next = ptr2 + ptr2 = ptr2.next + } + + tail = tail.next + } + + if (ptr1 != null) { + tail.next = ptr1 + } else if (ptr2 != null) { + tail.next = ptr2 + } + + return head.next + } +} \ No newline at end of file