Skip to content

Instantly share code, notes, and snippets.

@ObsidianCat
Last active May 8, 2020 10:55
Show Gist options
  • Save ObsidianCat/99eaea0ad30e70df95a2b7588e056219 to your computer and use it in GitHub Desktop.
Save ObsidianCat/99eaea0ad30e70df95a2b7588e056219 to your computer and use it in GitHub Desktop.
Accept array, convert it into linked list and return list head
function ListNode(val){
this.val = val;
this.next = null;
}
function createList(arr){
let result = arr.map((item, index, arr)=>{
const node = new ListNode(item)
return node
})
result = result.map((item, index, arr)=>{
if(index < (arr.length-1)){
item.next = arr[index+1]
}
return item
})
return result[0]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment