Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mergeSortedLists = (h1, h2) => {
- let head;
- let temp;
- if (!h1)
- return h2;
- if (!h2)
- return h1;
- while (h1 || h2) {
- if (!h2) {
- temp.next = new ListNode(h1.value, null);
- temp = temp.next;
- h1 = h1.next;
- } else if (!h1) {
- temp.next = new ListNode(h2.value, null);
- temp = temp.next;
- h2 = h2.next;
- } else if (!head && h1.value >= h2.value) {
- head = new ListNode(h2.value, null);
- temp = head;
- h2 = h2.next;
- } else if (head && h1.value >= h2.value) {
- temp.next = new ListNode(h2.value, null);
- temp = temp.next
- h2 = h2.next;
- } else if (!head && h1.value <= h2.value) {
- head = new ListNode(h1.value, null);
- temp = head;
- h1 = h1.next;
- } else if (head && h1.value <= h2.value) {
- temp.next = new ListNode(h1.value, null);
- temp = temp.next
- h1 = h1.next;
- }
- }
- return head;
- }
Advertisement
Add Comment
Please, Sign In to add comment