Created
October 28, 2020 20:50
-
-
Save dabsclement/175e9426a9324cb2f59237ef57deec7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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