Skip to content

Instantly share code, notes, and snippets.

@bunker-inspector
Created July 17, 2019 05:32
Show Gist options
  • Save bunker-inspector/f840dbd1fc090c7fbc0f7425c4ff477b to your computer and use it in GitHub Desktop.
Save bunker-inspector/f840dbd1fc090c7fbc0f7425c4ff477b to your computer and use it in GitHub Desktop.
Leetcode Problem for Merging Sorted Linked Lists
(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