diff --git a/merge-two-sorted-lists/yeonjukim164.java b/merge-two-sorted-lists/yeonjukim164.java new file mode 100644 index 0000000000..c9e6a3fff9 --- /dev/null +++ b/merge-two-sorted-lists/yeonjukim164.java @@ -0,0 +1,25 @@ +class Solution { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + ListNode dummy = new ListNode(-1); + ListNode curr = dummy; + + while (list1 != null && list2 != null) { + if (list1.val <= list2.val) { + curr.next = list1; + list1 = list1.next; + } else { + curr.next = list2; + list2 = list2.next; + } + curr = curr.next; + } + + if (list1 != null) { + curr.next = list1; + } else if (list2 != null) { + curr.next = list2; + } + + return dummy.next; + } +}