Last active
May 8, 2020 10:55
-
-
Save ObsidianCat/99eaea0ad30e70df95a2b7588e056219 to your computer and use it in GitHub Desktop.
Accept array, convert it into linked list and return list head
This file contains hidden or 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; | |
} | |
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