Created
March 21, 2018 12:26
-
-
Save guangtuan/11537f6cd79301300ea6a9793556db6f to your computer and use it in GitHub Desktop.
Sth about linkedList in javaScript
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
function ListNode(val) { | |
this.val = val; | |
this.next = null; | |
} | |
const last = arr => arr[arr.length - 1]; | |
const head = arr => arr[0]; | |
const linkedListFromArray = array => head(array.reduce((acc, curr, currIdx, arr) => { | |
let node = new ListNode(curr); | |
if (last(acc)) { | |
last(acc).next = node; | |
} | |
acc.push(node); | |
return acc; | |
}, [])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment