Skip to content

Instantly share code, notes, and snippets.

@guangtuan
Created March 21, 2018 12:26
Show Gist options
  • Save guangtuan/11537f6cd79301300ea6a9793556db6f to your computer and use it in GitHub Desktop.
Save guangtuan/11537f6cd79301300ea6a9793556db6f to your computer and use it in GitHub Desktop.
Sth about linkedList in javaScript
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