Created
July 17, 2019 05:32
-
-
Save bunker-inspector/f840dbd1fc090c7fbc0f7425c4ff477b to your computer and use it in GitHub Desktop.
Leetcode Problem for Merging Sorted Linked Lists
This file contains 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
(def input '((1 4 5 9 9 9 9 9 9) | |
(1 3 4 12 13) | |
(2 6) | |
(58))) | |
(defn- list->indexed-map | |
[ls] | |
(->> ls | |
(map-indexed (fn [idx ls*] [idx ls*])) | |
(into {}))) | |
(def indexed-input (list->indexed-map input)) | |
(def head* (comp first second)) | |
(defn- pull-min | |
[indexed-input] | |
(let [min* (apply min (remove nil? (map head* indexed-input))) | |
[nxt-idx nxt-ls] (-> | |
(filter #(= min* (head* %)) indexed-input) | |
first)] | |
[(first nxt-ls) (update indexed-input nxt-idx rest)])) | |
(prn (loop [result [] | |
acc indexed-input] | |
(if (every? empty? (vals acc)) | |
result | |
(let [[nxt-val acc*] (pull-min acc)] | |
(recur (conj result nxt-val) acc*))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment