commit 0cd6eafc48cd3307dc47414f8f6c307d948a566f parent 571b1ba5f8283bf4480117cdd95ee0d67705d4ed Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Sun, 23 Apr 2023 17:16:45 -0500 Completed merge sorted lists using javascript Diffstat:
| A | merge-two-sorted-lists/merge-two-sorted-lists.js | | | 77 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 77 insertions(+), 0 deletions(-)
diff --git a/merge-two-sorted-lists/merge-two-sorted-lists.js b/merge-two-sorted-lists/merge-two-sorted-lists.js @@ -0,0 +1,77 @@ +//Merge two sorted lists together and return the resulting singly linked list. +//The time complexity of this algorithm is O(n) where n is the length of both +//the lists combined. +//Runtime: 60ms Beats: 88.75% +//Memory: 44.1MB Beats: 54.16% + +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} list1 + * @param {ListNode} list2 + * @return {ListNode} + */ +var mergeTwoLists = function(list1, list2) { + + + + if(list1 == null && list2 == null){ + return null; + } + if(list1 == null){ + return list2; + } + if(list2 == null){ + return list1; + } + const merged_list = new ListNode(); + var itr = merged_list; + var itr1 = new ListNode(); + while(list1 != null && list2 != null){ + itr1 = new ListNode(); + itr.next = itr1; + if(list1.val < list2.val){ + itr.val = list1.val; + list1 = list1.next; + } + else{ + itr.val = list2.val; + list2 = list2.next; + } + itr = itr1; + } + + while(list1 != null){ + if(list1.next != null){ + itr1 = new ListNode(); + itr.next = itr1; + itr.val = list1.val; + list1 = list1.next; + itr = itr1; + } + else{ + itr.val = list1.val; + break; + } + } + while(list2 != null){ + if(list2.next != null){ + itr.next = itr1; + itr1 = new ListNode(); + itr.val = list2.val; + list2 = list2.next; + itr.next = itr1; + itr = itr1; + } + else{ + itr.val = list2.val; + break; + } + } + return merged_list; +};