Skip to content

Instantly share code, notes, and snippets.

@dabsclement
Created October 28, 2020 20:50
Show Gist options
  • Save dabsclement/175e9426a9324cb2f59237ef57deec7e to your computer and use it in GitHub Desktop.
Save dabsclement/175e9426a9324cb2f59237ef57deec7e to your computer and use it in GitHub Desktop.
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
let mergeTwoLists = function(l1, l2) {
if (!l1 || !l2) return l1 || l2;
const linkThem = (smaller, greater) => {
smaller.next = mergeTwoLists(smaller.next, greater);
return smaller;
};
return l1.val < l2.val ? linkThem(l1,l2) : linkThem(l2,l1);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment